Android Listview in PopupWindow onitemclick not work on some device

混江龙づ霸主 提交于 2019-12-05 00:45:08
govind kumar Gupta

You can make context menu instead of popup menu it work same like this . For creating context menu just refer this link:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

In this link refer to Creating Contextual Menus.

Sort exanple on this like this u can add context menu ------ first register it in onclicklistener like

registerForContextMenu(view)

for oncreate  
    @Override     
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)  
 {  
        menu.add(Menu.NONE, CONTEXT_MENU_UNPAIR_ITEM, Menu.NONE, "UNPAIR");
        menu.add(Menu.NONE, DEFAULT_DEVICE, Menu.NONE, "USE AS CGM DEVICE");        
    }

For item selected in context menu  
@Override  
public boolean onContextItemSelected(MenuItem item)  
{  
 switch (item.getItemId())  
{  
 case CONTEXT_MENU_UNPAIR_ITEM:  
 //whatever u want
break  
case DEFAULT_DEVICE:  
 //whatever your logic accordind to u  
return(true);  
}  
return(super.onOptionsItemSelected(item));  
}                            

You may want to use ListAdapter instead of BaseAdapter for a ListView , and return True for areAllItemsEnabled and isItemEnabled in the class that implements ListAdapter.

Something like this...

class CustomListAdapter implements ListAdapter {

    Activity callingActivity;
    ArrayList<String> items;

    public CustomListAdapter(Activity act, ArrayList<String> list)
    {
        callingActivity = act;
        items = list;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) { 
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);

        LinearLayout ll = new LinearLayout(callingActivity);
        ll.setLayoutParams(params);
        ll.setGravity(Gravity.CENTER);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setPadding(5, 5, 5, 5);

        //
        // Code for your list item here...
        //

        return ll;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

}

Good Luck. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!