嗨,我坚持使用复选框从列表视图中删除多个项目。我可以从列表视图中选择多个项目,但只有第一个被选中的项目被删除,而其他项目没有被删除。这是我的代码。提前致谢!
public class MainActivity extends Activity
{
ListView lv;
ArrayAdapter<String> adapter;
Button delete;
ArrayList<String> data = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
delete = (Button)findViewById(R.id.button1);
data.add("Windows");
data.add("Android");
data.add("Apple");
data.add("Blackberry");
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, data);
lv.setAdapter(adapter);
delete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
deleteCheckedItems();
adapter.notifyDataSetChanged();
}
});
}
@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;
}
private void deleteCheckedItems() {
int count = adapter.getCount();
for (int i = 0; i < count; i++)
{
if (lv.isItemChecked(i))
{
data.remove(i);
}
}
}
}