我正在使用数组适配器在列表视图中显示数组列表。我可以添加和删除项目。假设如果列表视图中没有项目,如果我选择从绑定异常中删除其显示的索引。我需要的是它应该显示像“没有要删除的项目”这样的吐司。澄清我的专家!我的代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bAdd = (Button) findViewById(R.id.button1);
bDel = (Button) findViewById(R.id.button2);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.EditText2);
et3 = (EditText) findViewById(R.id.EditText3);
lv = (ListView) findViewById(R.id.listView1);
al = new ArrayList<String>();
aa = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, al);
lv.setAdapter(aa);
bAdd.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String str1 = et1.getText().toString();
if (str1.equals("")) {
Toast.makeText(getApplicationContext(),
"Please Enter Item first!!", 0).show();
} else {
al.add(0, str1);
aa.notifyDataSetChanged();
et1.setText("");
}
}
});
bDel.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0 == null) {
Toast.makeText(getApplicationContext(),
"Nothing to delete", 0).show();
} else {
al.remove(0);
aa.notifyDataSetChanged();
}
}
});