Milan Gajera Milan Gajera - 4 months ago 19
Android Question

How can i check the checkbox onItemClickListener event of listivew in android?

Explanation:
I tried more but not got the perfect solution as per my expectation.
I want to check by checbox on the click of listview row in BaseAdapter??.

AcCategoryAdapter.java

public class AcCategoryAdapter extends BaseAdapter{

private Activity activity;
private List<AcCategory> acCategoryList;
private LayoutInflater inflater;

public AcCategoryAdapter(Activity activity,List<AcCategory> acCategoryList){
this.activity=activity;
this.acCategoryList=acCategoryList;
inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return this.acCategoryList.size();
}

@Override
public Object getItem(int position) {
return this.acCategoryList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

private class Holder{
TextView txtCategoryName;
ImageView ivImg;
CheckBox cbCooling;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

final Holder holder;
if(convertView==null){
holder=new Holder();
convertView=inflater.inflate(R.layout.ac_problems_list,parent,false);

holder.txtCategoryName=(TextView)convertView.findViewById(R.id.txt_cooling);
holder.ivImg=(ImageView)convertView.findViewById(R.id.iv_img);
holder.cbCooling=(CheckBox)convertView.findViewById(R.id.cb_cooling);

convertView.setTag(holder);
}
else{
holder=(Holder)convertView.getTag();
}
final AcCategory acCategory=acCategoryList.get(position);
holder.txtCategoryName.setText(acCategory.getName());

// convertView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(activity,acCategory.getName(), Toast.LENGTH_SHORT).show();
// holder.cbCooling.setChecked(true);
// }
// });
return convertView;
}


list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_cooling"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:orientation="horizontal"
android:padding="10dp">

<ImageView
android:id="@+id/iv_img"
android:layout_width="25dp"
android:layout_height="25dp"
android:contentDescription="@string/img_desc"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_cooling" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">

<TextView
android:id="@+id/txt_cooling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="@string/txt_cooling"
android:textSize="18sp" />

<CheckBox
android:id="@+id/cb_cooling"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:button="@drawable/checkbox_checked"
android:checked="false" />
</RelativeLayout>

</LinearLayout>


above is the list row where i have ImageView,TextView and CheckBox and it is inflated into my adapter

Here is the call of adapter from the activity

lvProblem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG,"LISTVIEW SELECTED ITEM:-"+acCategoryList.get(position).getName());
}
});


Additional, i get the functionality like it's check from the onItemClickListener and checkbox itself.

Please, help me to solve out this problem.

Answer
  1. You may add a parameter in AcCategory.class, just like:

    boolean isChecked = false;
    
  2. You need to set the isChecked variable to true/false in OnItemClickListener

    lvProblem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // retrieve your relative item
            <your adapter>.getItem(position).isChecked = !<your adapter>.getItem(position).isChecked;
        }
    });
    
  3. In your adapter, you need to add the checkbox in your getView method

    <your check box variable>.setChecked(acCategory.isChecked);
    
  4. Everytime you make change, you need to call

    <your adapter variable>.notifyDataSetChanged();