Android how to identify item in listview with checkbox

半城伤御伤魂 提交于 2019-11-27 02:03:00

This is XML for Custom Row in ListView :

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/camera_icon" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" 
     />

</LinearLayout>

This is Complete Activity in which List is implemented : Just Copy this Activity for test then understand code. Place one listview in main Activity

package com.DemoTest;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CustomList extends Activity implements OnClickListener
{

    ListView listView;
    ArrayList<EachRow> list=new ArrayList<CustomList.EachRow>();
    EachRow each;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String[] color={"red","green","blue","white","yellow","cyan","purple","grey",
                "0red","0green","0blue","0white","0yellow","0cyan","0purple","0grey",
                "1red","1green","1blue","1white","1yellow","1cyan","1purple","1grey"};
        for(String str : color)
        {
            each=new EachRow();
            each.text=str;
            list.add(each);
        }
        listView=(ListView)findViewById(R.id.listView1);
        listView.setAdapter(new MyAdapter(this, 0, list)); 
        //listView.setOnItemClickListener(this);
    }
    class MyAdapter extends ArrayAdapter<EachRow>
    {
        LayoutInflater inflat;
        ViewHolder holder;
        public MyAdapter(Context context, int textViewResourceId,
                ArrayList<EachRow> objects) 
        {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
            inflat=LayoutInflater.from(context);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null)
            {
                convertView=inflat.inflate(R.layout.row_checkox, null);
                holder=new ViewHolder();
                holder.textView=(TextView)convertView.findViewById(R.id.textView1);
                holder.image=(ImageView)convertView.findViewById(R.id.imageView1);
                holder.check=(CheckBox)convertView.findViewById(R.id.checkBox1);
                holder.check.setOnClickListener(CustomList.this);
                convertView.setTag(holder);
            }
            holder=(ViewHolder) convertView.getTag();
            EachRow row= getItem(position);
            Log.d("size", row.text);
            holder.textView.setText(row.text); 
            holder.check.setChecked(row.checkBool); 
            holder.check.setTag(position);
            return convertView;
        }

        @Override
        public EachRow getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        private class ViewHolder
        {
            TextView textView;
            ImageView image;
            CheckBox check;
        }
    }
    private class EachRow
    {
        String text;
        boolean checkBool;
    }



    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub

        EachRow row=list.get((Integer)v.getTag());
         row.checkBool=!row.checkBool;
         Log.d("item", "Item Click at "+(Integer)v.getTag()+" : "+row.text+" is "+row.checkBool); 
    }

}
Snicolas

Better than adding an onClickListener to each checkbox, you could also use an ItemClickListener on your list.

Refer to this post to find out the row that has been clicked : onItemClick <string-array> strings.xml (not ListView/ArrayList)

There are other options, but it involves creating a custom row widget class, and passing it its row position when your adapter creates or updates it.

you would have to have a onTouchListener on the container that holds all of them and make sure that you return false on your listeners so that they don't consume the event. onClickListener consumes it automatically.

If you only concerned with text and checkbox then you can use this

In XML :

<ListView
    android:id="@+id/listViewCursor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice" >

String[] color={"red","green","blue","white","yellow","cyan","purple","grey"};

list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, color));

ANd on some event check which checkboxes are selected.

SparseBooleanArray boolArray=list.getCheckedItemPositions();
        int size=list.getCount();
        for(int i=0;i<size;i++)
        {
            if(boolArray.valueAt(i)==true)
            {
                Log.d("size", color[i]);
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!