当前位置: 动力学知识库 > 问答 > 编程问答 >

android - 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: