【发布时间】:2015-06-10 08:48:38
【问题描述】:
当在列表元素的布局上单击按钮 x 时,我想从列表中删除一个元素。 这是我的 java 代码, onNext() 用于将元素添加到 list 。但是 onDelete 不起作用。添加新元素时,textView4 元素的数字从 1 到 n 开始
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list2);
arrayList = new ArrayList<String>();
arrayList.add("1");
adapter2 = new ArrayAdapter<String>(this, R.layout.list2_layout, R.id.textView4, arrayList);
ListView myList2 = (ListView)
findViewById(R.id.listView2);
myList2.setAdapter(adapter2);
}
public void onNextItem(View v)
{
counter++;
String str=Integer.toString(counter);
arrayList.add(str);
adapter2.notifyDataSetChanged();
}
public void onDeleteItem(View v2)
{
arrayList.remove(R.id.textView4);
adapter2.notifyDataSetChanged();
}
【问题讨论】:
-
如果您只想删除最后添加的项目,请使用:
arrayList.remove(arrayList.size()-1);