我有两个 EditText 字段,即名称和标记以及一个添加按钮。
每次单击“添加”按钮时,我都必须显示 EditText 值。
但是,我只能在列表视图上显示一个值。
当我再次单击“添加”按钮时,其先前的值将被删除,而较新的值将显示在列表视图中。
我想在列表视图中填充整个列表。
public class MainActivity extends Activity {
EditText name1;
EditText marks1;
private ListView lv;
ArrayAdapter<String> aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView markshee = (TextView)findViewById(R.id.textView3);
markshee.setText("");
Button btnAdd = (Button) findViewById(R.id.button1);
btnAdd.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try{
name1 = (EditText)findViewById(R.id.editText1);
String name = name1.getText().toString();
marks1 = (EditText)findViewById(R.id.editText2);
String marks = marks1.getText().toString();
if(name.equals("") || marks.equals("")){
String str="Don't Leave any field blank !";
Toast toast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else {
TextView marksheet = (TextView)findViewById(R.id.textView3);
marksheet.setText("Marks Sheet");
marksheet.setTextColor(Color.BLUE);
TextView nam = (TextView)findViewById(R.id.textView4);
nam.setText("Name");
nam.setTextColor(Color.RED);
TextView mar = (TextView)findViewById(R.id.textView5);
mar.setText("Marks");
mar.setTextColor(Color.RED);
name1.setText("");
marks1.setText("");
lv = (ListView) findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
ArrayList<String> data = new ArrayList<String>();
data.add(" "+name+" "+marks);
aa =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, data);
lv.setAdapter(aa);
}
}catch(Exception ex)
{
System.out.println(ex.getStackTrace());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}