如何在android中检查listivew的onItemClickListener事件复选框?

时间:2016-08-04 11:56:57

标签: android

说明:            我尝试了更多,但没有按照我的期望获得完美的解决方案。 我想通过在BaseAdapter中单击listview行来检查checbox。

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>

上面是列表行,我有ImageView,TextView和CheckBox,它被充气到我的适配器

以下是来自活动的适配器调用

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());
            }
        });

另外,我从onItemClickListener和复选框本身获得了类似它的功能。

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

  1. 您可以在AcCategory.class中添加参数,如:

    boolean isChecked = false;
    
  2. 您需要在OnItemClickListener中将isChecked变量设置为true / false

    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. 在适配器中,您需要在getView方法中添加复选框

    <your check box variable>.setChecked(acCategory.isChecked);
    
  4. 每次进行更改时,都需要致电

    <your adapter variable>.notifyDataSetChanged();
    

答案 1 :(得分:0)

你必须创建一个字节变量。当你点击复选框并将其cheked然后设置其值,否则设置为零。并检查它在bindview上的值。 并通知你的清单。