Android: Get the listview item from button clicked in custom listview

后端 未结 5 587
终归单人心
终归单人心 2021-02-02 16:17

I have a custom ListView with two button and I when I click either button on any row I want to get the text label on the Listview and for now just popup a toast with it. So far

5条回答
  •  迷失自我
    2021-02-02 16:58

    use setTag attribute of the View..............

    as

    Button call = (Button) rowView.findViewById(R.id.button1);
    call.setTag(position);
    

    and

    call.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                   int which = -1;
                   Obejct obj =  v.getTag();
                  if(obj instaceof Integer){
                   which  = ((Integer)obj).intValue();
                        }
    
                  if(which >-1){
                    String name = values[which];
                    Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
                    }
                }
            });
    

提交回复
热议问题