Thursday, 5 September 2013

retrieving the list value using contextmenu

retrieving the list value using contextmenu

I am developing an android application.I will have a listview and i have
set a context menu to appear when a listview item is long-pressed.How do i
get the item from the listview item selected(say text from a listview
textview) after an action from the contextmenu is chosen so i can process
it? Here is the code:
public class HomeActivity extends Activity implements OnItemClickListener {
ListView lv1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
lv1 = (ListView) findViewById(R.id.listings);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.watch:
String name = "";
Intent intent = new Intent(HomeActivity.this, Watch.class);
startActivity(intent);
return true;
case R.id.buy:
return true;
default:
return super.onContextItemSelected(item);
}
}
In the onContextItemSelected method,i want to obtain a value from the
specific clicked list item textview.How do i achieve this.Please help,i
have had a lot of trouble trying it.Thank you.

No comments:

Post a Comment