which event should i use TextView or ListView event
I have a list of contacts available in list view now when ever user click on list i want to show edit option, as i am new to android 开发者_运维问答i dont know on which event i should write code.
i tried with listview event as setOnItemClickListener but its giving me error when i tried to used suggested option but still its showing me error please help me.
Use something like this:
// In OnCreate() call this
registerForContextMenu(yourlistview);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
menu.setHeaderTitle("Choose an option");
menu.add(0, 0, 0, "Edit");
menu.add(0, 1, 0, "Something Else");
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
switch ( item.getItemId() )
{
case 0:
ListView listView = (ListView)findViewById(R.id.yourlist);
// In my example i've used a string. Do not use copy pasta. yeah ?
String S = (String) listView.getAdapter().getItem(menuInfo.position);
This is a context menu. Read up on it. You'll get the idea.
精彩评论