Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 nothing has worked I keep getting the last item in my array.

Here is a screen shot to give you a better idea of what i mean

enter image description here

Here is my Adapter subclass for my custom ListView

static final String[] Names = 
           new String[] { "John", "Mike", "Maria", "Miguel"};


class MyArrayAdapter extends ArrayAdapter<String> {
    private final Context context;      

    int which;

    public MyArrayAdapter(Context context, String[] pValues) {
        super(context, R.layout.main, pValues);
        this.context = context;
        values = pValues;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rowView = inflater.inflate(R.layout.main, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        Button call = (Button) rowView.findViewById(R.id.button1);
        Button chat = (Button) rowView.findViewById(R.id.button2);
        textView.setText(values[position]);


        // Change icon based on name
        s = values[position];           
        which = position;
        call.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String name = values[which];
                Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
            }
        });

        return rowView;
    }
}

Edit:

String name = textView.getText().toString();
RelativeLayout ll = (RelativeLayout)v.getParent();
textView = (TextView)ll.findViewById(R.id.label);
Toast.makeText(CustomListView.this, name,
Toast.LENGTH_SHORT).show();
like image 293
Amanni Avatar asked Jun 22 '12 12:06

Amanni


2 Answers

Easy to do:

    call.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
            RelativeLayout rl = (RelativeLayout)v.getParent();
            TextView tv = (TextView)rl.findViewById(R.id.label);
            String text = tv.getText().toString();
            Toast.makeText(CustomListView.this, text, Toast.LENGTH_SHORT).show(); 
        } 
    }); 
like image 195
Barak Avatar answered Oct 14 '22 23:10

Barak


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();
                }
            }
        });
like image 36
Dheeresh Singh Avatar answered Oct 14 '22 23:10

Dheeresh Singh