Reputation: 2333
I have dialog in which listview is being displayed.On long pressing items in list context menu pops-up.Now on pressing delete in Contextmenu should delete corresponding item in listview. Here is my code i have written so far...
public class ViewContacts extends Activity {
List<String> contacts;
SharedPreferences prefs;
SharedPreferences.Editor editor;
ArrayAdapter<String> adapter;
Dialog dialog;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
contacts = new ArrayList<String>();
dialog = new Dialog(ViewContacts.this);
dialog.setTitle("My Contacts");
dialog.setContentView(R.layout.activity_mycontacts);
dialog.setCancelable(true);
dialog.show();
contacts.add("Abc");
contacts.add("xyx");
contacts.add("klm");
lv =(ListView)dialog.findViewById(R.id.lv);
adapter =new ArrayAdapter<String>(this,R.layout.row,R.id.contacts,contacts);
lv.setAdapter(adapter);
registerForContextMenu(lv);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater =getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.del_menu:
contacts.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
return true;
case R.id.can_menu:
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
menu_list.xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/del_menu"
android:title="Delete">
</item>
<item android:id="@+id/can_menu"
android:title="Cancel">
</item>
</menu>
My code is not doing anything.What could be the problem?Any suggestions or alternative to this approach of deleting items from listview?
Upvotes: 1
Views: 2049
Reputation: 2333
I solved that problem by using OnItemlongclicklistener.Thanks @Dhara Shah for suggestion..
This is my code..
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(ViewContacts.this,Delete_Confirm.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
//I am sending position of listitem in putExtra below//
i.putExtra("position", position);
startActivityForResult(i,CONFIRM);
return true;
}
});
In this Delete_Confirm class is Activity containing custom dialog for getting confirmation from user whether to delete it or not..
This is how my Delete_Confirm.java code:
public class Delete_Confirm extends Activity implements OnClickListener {
Dialog dialog;
Button del,cancel;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
pos = bundle.getInt("position");
Log.d("msg","position is " + pos);
dialog = new Dialog(Delete_Confirm.this);
dialog.setTitle("My Contacts");
dialog.setContentView(R.layout.activity_del);
del = (Button) dialog.findViewById(R.id.del_con);
cancel = (Button) dialog.findViewById(R.id.no_del_con);
del.setOnClickListener(this);
cancel.setOnClickListener(this);
dialog.setCancelable(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
finish();
}
});
dialog.show();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.del_con:
Intent yes = this.getIntent();
yes.putExtra("position", pos);
this.setResult(RESULT_OK,yes);
finish();
break;
case R.id.no_del_con:
Intent no = this.getIntent();
this.setResult(RESULT_CANCELED,no);
finish();
break;
default:
break;
}
}
Now in onActivityResult i am getting user's choice from the Delete_Confirm.java's setResult() method.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CONFIRM){
if(resultCode==RESULT_OK){
int posi = data.getIntExtra("position",0);
Log.d("msg","positionwa is " + posi);
Log.d("msg","Do we reach here?");
if(posi==0){
contacts.remove(posi);
adapter.notifyDataSetChanged();
}
else if(posi==1){
contacts.remove(posi);
adapter.notifyDataSetChanged();
}
else if(posi==2){
contacts.remove(posi);
adapter.notifyDataSetChanged();
}
}
if(resultCode==RESULT_CANCELED){
dialog.dismiss();
finish();
}
}
I posted my answer in case someone faces the same problem..
Upvotes: 1
Reputation: 7259
A long item click listener has the position.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// TODO Auto-generated method stub
// store the position of the item that has to be deleted
return true;
}
});
When the delete in the context menu is pressed or tapped, since you have the position, in that case remove the item from the list like
list.remove(position);
and call notifyDatasetChanged()
again.
Upvotes: 0