ListView subobject clickable confilct

You need to understand how listview recycle mechanism works

How ListView’s recycling mechanism works

Use a Model Class. Assume you already have the below

public class FeedItem {

String title,content;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

}

In getView

holder.accept.setText(listData.get(position).getContent()); 
holder.accept.setTag(position);
holder.accept.setOnClickListener(mClickListener);

Then

private OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) {
    int pos = (Integer) v.getTag();
    FeedItem newsItem = (FeedItem) listData.get(pos);
    newsItem.setContent("Accepted");
    CustomListadapter.this.notifyDataSetChanged();
}
};

Exaplanation :

You use a model class which has getters and setters.

You setTag to the button with position. In onClick you get the tag ie position and change the content accordingly. You refresh listview by calling notifyDataSetChanged on the adapter.

For the benefit of others here’s a example

public class MainActivity extends Activity {

   ArrayList<Holder> list = new ArrayList<Holder>();
   ListView lv;
   CustomListAdapter cus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        for(int i=0;i<10;i++)
        {
            Holder h = new Holder();
            h.setTitle("Title"+i);
            h.setContent("Content"+i);
            h.setColor(Color.BLACK);
            list.add(h);
        }
        cus = new CustomListAdapter(this,list);
        lv.setAdapter(cus);
    }
}

Model class Holder

public class Holder {

    String title,content;
    int color;

    public int getColor() {
return color;

    public void setColor(int color) {
this.color = color;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

CustomListAdapter

public class CustomListAdapter extends BaseAdapter{

    LayoutInflater inflater;
    ArrayList<Holder> list;
    public CustomListAdapter(MainActivity mainActivity, ArrayList<Holder> list) {
        inflater = LayoutInflater.from(mainActivity);
        this.list =list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) { 
        ViewHolder holder; 
        if (convertView == null) { 
            convertView = inflater.inflate(R.layout.list_item, 
                    parent, false);
            holder = new ViewHolder(); 
            holder.tv = (TextView) convertView.findViewById(R.id.textView1); 
            holder.b = (Button) convertView.findViewById(R.id.button1);
           convertView.setTag(holder); 
       } else { 
           holder = (ViewHolder) convertView.getTag(); 
       } 
       Holder h = list.get(position);
       holder.tv.setText(h.getTitle());
       holder.b.setText(h.getContent());
       holder.b.setTextColor(h.getColor());
       holder.b.setOnClickListener(mClickListener); 
       holder.b.setTag(position);
       return convertView; 
}
     private OnClickListener mClickListener = new OnClickListener() {

            public void onClick(View v) {
                int pos = (Integer) v.getTag();
                Holder h = (Holder) list.get(pos);
                h.setContent("Accepted");
                    h.setColor(Color.BLUE);
                CustomListAdapter.this.notifyDataSetChanged();

            }

            };
    static class ViewHolder
    {
        TextView tv;
        Button b;
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="40dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="22dp"
        android:text="TextView" />

</RelativeLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </ListView>

</RelativeLayout>

Snap

Button at row 1 and 5 is clicked so it is changed to Accepted and is Blue.

enter image description here

Leave a Comment