开发者

which event should i use TextView or ListView event

开发者 https://www.devze.com 2023-02-15 01:22 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号