Reputation: 2398
I have two Edit Text in each item of listView. When user long press on any item in listView I am showing a contextMenu and giving two options Edit and Delete now how do I know that on which item in listView user long pressed to open the contextmenu.
XML of each item of ListView
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/templateId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/templateTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
XML for context menu
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/editTemplate"
android:title="Edit" />
<item android:id="@+id/saveTemplate"
android:title="Save" />
<item android:id="@+id/deleTemplate"
android:title="Delete" />
Code
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Delete");
TextView tv=(TextView)v.findViewById(R.id.templateId);
selectedId=tv.getText().toString();
TextView tvMessage=(TextView)v.findViewById(R.id.templateTextId);
selectedTemplate=tvMessage.getText().toString();
//Toast.makeText(getApplicationContext(), "Item In List View Clicked ",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Edit")
{
// Toast.makeText(ShowTemplates.this, "Edit Clicked",Toast.LENGTH_SHORT).show();
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(ShowTemplates.this);
dialog.setContentView(R.layout.custome_dialog_edit_template);
dialog.setTitle("Edit Template");
txtMsgTemplate = (EditText) dialog.findViewById(R.id.editTextTemplateCustomDialog);
txtMsgTemplate.setText(selectedTemplate);
Button btnSave=(Button)dialog.findViewById(R.id.btnSaveEditedTemplate);
dialog.show();
}
I tried to find it and I got following
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// Get the Adapter behind your ListView (this assumes you're using
// a ListActivity; if you're not, you'll have to store the Adapter yourself
// in some way that can be accessed here.)
Adapter adapter = getListAdapter();
// Retrieve the item that was clicked on
Object item = adapter.getItem(info.position);
}
But I dont know how to use this Item object. Is there any other way to do that. Thanks
Upvotes: 0
Views: 1578
Reputation: 1741
ListViews have a function called getSelectedItemPosition
that returns an int with the item's position in the adapter. I am pretty sure you could use that. If that is null in onCreateContextMenu
then try grabbing a reference to it in your onLongClick listener
.
Upvotes: 1