3

I have the following ListView with a custom list item:

custom_list_item

I would like to get the TextView values from the list item where the ToggleButton is clicked.

Here's the adapter that builds the list:

    public class simpleAdapter extends SimpleAdapter {

    public simpleAdapter(Context context,
            ArrayList<HashMap<String, String>> list, int resource,
            String[] from, int[] to) {
        super(context, list, resource, from, to);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // return super.getView(position, convertView, parent);

        LayoutInflater inflater = getLayoutInflater();
        convertView = inflater.inflate(R.layout.user_campaigns_list_item, parent, false);

        tb_campaigns_list_toggle = (ToggleButton) convertView
                .findViewById(R.id.tb_campaigns_list_toggle);
        tb_campaigns_list_toggle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });

        return super.getView(position, convertView, parent);

    }

}

With this code, I'm able to toast the position of the list item containing the ToggleButton clicked. When trying to get the Text1 or id1 values using this code:

TextView Text1 = (TextView)convertView.findViewById(R.id.Text1_id); //this line is outside the onClick(View v) method
String Text1_value = Text1.getText().toString();
Toast.makeText(getApplicationContext(), Text1_value, Toast.LENGTH_SHORT).show();

I'm always getting the value of the Text1 from the last ListView item.

any ideas?

Andrei Stalbe
  • 1,511
  • 6
  • 26
  • 44

2 Answers2

8

In onItemClick function of the listview, Paste the following code:

    View parentView = (View) view.getParent();
    textview1 = ((TextView) parentView
            .findViewById(R.id.textview1)).getText().toString();

    textview2 = ((TextView) parentView
            .findViewById(R.id.textview2)).getText().toString();

    textview3 = ((TextView) parentView
            .findViewById(R.id.textview3)).getText().toString();

You can get any value using the above code..

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54
1

use setTag and GetTag of button

tb_campaigns_list_toggle = (ToggleButton) convertView
                    .findViewById(R.id.tb_campaigns_list_toggle);

    tb_campaigns_list_toggle.setTag(position);//<-------------


            tb_campaigns_list_toggle.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                        int index = -1;
                         Object obj =   v.getTag();//<-------------

                             if(obj instanceOf Integer){
                                  index = ((Integer)obj).getIntValue(); 
                              }

                    Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
                }
            });
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • Your answer confuses me a bit because what I would like to do is to get the `TextView` values. Looking at your code it isn't clear (at least for me) how to get the values of the `Text1` and `id1`. – Andrei Stalbe Jun 25 '12 at 11:57
  • yup that depends on passing the data to adapter as we generally used adapter.getItem(position); – Dheeresh Singh Jun 25 '12 at 12:09
  • @DheereshSingh sir it want get the text from the list view ,on click Toggle Button which is chid of the list view how to get text . can u help me sir. – Rishi Gautam Dec 28 '12 at 04:59