如何使用onCreateContextMenu从ListView中删除项目?下面的代码中存在一些问题。任何解决方案?
当长按列表视图中的项目时,会弹出一个菜单,其中有一个删除选项。
public class ContextMenuTest extends AppCompatActivity {
ListView listView;
String []name={"ANKUSH", "ANOUSHKA", "SHIVA", "SOMANSHU"};
String current;
ArrayAdapter<String> adapter;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_menu_test);
listView = (ListView) findViewById(R.id.lvContextMenu);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("SELECT OPTION");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_test, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete:
adapter.remove(adapter.getItem(info.position));
break;
}
return true;
}
}
答案 0 :(得分:0)
在onContextItemSelected
方法中,添加adapter.notifyDataSetChanged()
以通知适配器数据更改。
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete:
adapter.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
break;
}
return true;
}
答案 1 :(得分:0)
要使用onCreateContextMenu从ListView中删除项目,您可以这样做:
int currentposition;
listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
currentposition = info.position;
menu.setHeaderTitle("SELECT OPTION");
menu.add(0, v.getId(), 0, "Delete ");
}
});
然后:
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
if (item.getTitle() == "Delete ") {
mArray.remove(currentposition);
ca.notifyDataSetChanged();
Toast.makeText(this, "Deleted ", Toast.LENGTH_SHORT)
.show();
}
return true;
}
答案 2 :(得分:0)
本作品
public class ContextMenuTest extends AppCompatActivity {
ListView listView;
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_menu_test);
listView = (ListView) findViewById(R.id.lvContextMenu);
list.add("ANKUSH");
list.add("ANOUSHKA");
list.add("SHIVA");
list.add("SOMANSHU");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("SELECT OPTION");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_test, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete:
list.remove(info.position);
adapter.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
}