我使用數組適配器來顯示列表視圖中的數組列表。我可以添加和刪除項目。假設如果在列表視圖中沒有項目,如果我選擇刪除其顯示索引超出了綁定異常。我需要的是它應該顯示祝酒像「沒有項目刪除」。澄清我的專家!我的代碼如下:Android:索引超出ArrayList的例外
@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();
}
}
});
請出示您的logcat的 – ashutosh