OnItemCLickListener not working in listview

前端 未结 24 1967
悲&欢浪女
悲&欢浪女 2020-11-22 11:21

Activity class code:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  Conver         


        
24条回答
  •  逝去的感伤
    2020-11-22 11:49

    private AdapterView.OnItemClickListener onItemClickListener;
    
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
     .......
    
    final View view = convertView;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
             if (onItemClickListener != null) {
                 onItemClickListener.onItemClick(null, view, position, -1);
             }
          }
     });
    
    return convertView;
    }
    
    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    

    Then in your activity, use adapter.setOnItemClickListener() before attaching it to the listview.

    Copied from github its worked for me

提交回复
热议问题