我有这段代码,我习惯将数组列表传递到另一个页面并将其显示为 ListView 。当列表出现时,我希望能够检查一个项目并在“单击按钮”时将其删除,这将修改数组。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oppout);
final ListView lv2 = (ListView) findViewById (R.id.custom_list_view);
lv2.setClickable(true);
lv2.setAdapter(new ArrayAdapter<String>(Oppout.this,
android.R.layout.simple_list_item_checked,
Entername.playerList));
lv2.setOnItemClickListener (
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView,
View view,int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "mu"+ selectedPosition,
Toast.LENGTH_SHORT).show();
}
});
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// on click call the adapterview and delete string at selected position.
// This is my problem, am not getting how to call the adapter and deleted
// the selected item/position
int selectedPosition = adapterView.getSelectedItemPosition();
adapterView.remove(player.SelectedPosition);
Intent myIntent = new Intent (view.getContext(), Callacab.class);
startActivityForResult(myIntent, 0);
}
});
}}
最佳答案
联系人删除(索引);//你给你的适配器的arraylist
arrayadapter.remove(index);//这是你给 ListView 的适配器 arrayadapter.notifyDataSetChanged();
//你可以从你的数组列表或你的适配器中删除然后notifyDataSetChanged();使效果发生在你的 ListView 中....最好从 arraylist 中删除,它的索引将是你的 ListView 项目监听器中的 arg2
关于android - 如何从数组列表/ ListView 中删除选中的项目/特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9557458/