I\'m trying to make a listmanager application in Android. I made a ListView, and an ArrayList into which I can add items with a button and an EditText. Then I made a context
I wrote above adapter code in my program, when i run program & delete item using context menu at that time force close window come
this is my code
public class ShowContextMenu extends ListActivity {
ArrayAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String []s=(getResources().getStringArray(R.array.names));
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,s);
setListAdapter(adapter);
registerForContextMenu(getListView());
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
String[] names = getResources().getStringArray(R.array.names);
switch(item.getItemId()) {
case R.id.edit:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.edit) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
case R.id.save:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
// insert code here
adapter.remove(adapter.getItem(info.position));
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.delete) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
case R.id.view:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.view) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
}