简体   繁体   中英

Problem getting Checkbox items on the ListView

I used before setListAdapter with simple_multiple_choice to make listview with checkboxes.

And then I did to get size and chosed positions:

SparseBooleanArray checked = list.getCheckedItemPositions();
cont = list.getCheckedItemPositions().size();

if (checked.get(i)) 
{
...

But now I needed more control on the layout, so i'm doing the listview on the xml, something like:

<listview>
<checkboxes id=.../>
</listview>

using setAdapter to those ids.

The listview is correctlyt populated (as before), problem is , now the SparseBooleanArray don't work. Thar variable 'cont', gives me 0, and 'checked' is null.

To make sure that 'list' is ok:

int len = list.getCount();

It gives me a right value.

What's the problem?

You need to Implement custom List Adapter.

public class YourAdapterName extends BaseAdapter{

private Context mContext;
private DataType mValuestoShow;//Use your DataType to pass values to adapter. 

/**
 * Constructor to be called to initialize adapter with values.
 * @param context
 * @param vector
 */
public YourAdapterName(Context context, DataType data){
    mContext = context;
    mValuestoShow = vector;
}

public int getCount() {
    if(null != mValuestoShow){
        return mValuestoShow.size();
    }
    return 0;
}

public Object getItem(int position) {
    if(position < mValuestoShow.size())
        return  mValuestoShow.get(position);
    else
        return null;
}

public long getItemId(int position) {
    return 0;
}



public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder ;
    if(convertView == null){
        LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.your_layout, null);
        holder = new ViewHolder();
        holder.txt_name = (TextView) convertView.findViewById(R.id.name_txt);
        holder.checkBox = (Checkbox) convertView.findViewById(R.id.checkbox);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt_name.setText(getItem(position).toString());
    holder.checkBox    // Do your task with checkbox.
    return convertView;
}

class ViewHolder {
    TextView txt_name;
    Checkbox checkBox;
}

}

your_layout.xml

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

<TextView
    android:id = "@+id/txt_type1"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />

<CheckBox
    android:id = "@+id/checkbox"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_alignParentRight="true" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM