简体   繁体   中英

Set checkbox state in listview Adapter

I have a few checkboxes that are selected when a user builds a car. Then in my listview adapter i'm trying to account for this. Below is my code, I don't think "setText" will work for a checkbox.

This is my error:

android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:274)
at android.widget.TextView.setText(TextView.java:4122)

Here is my listview adapter code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if(v == null) {
        v = mInflater.inflate(R.layout.list_item_car, parent, false);
        holder = new ViewHolder();
        holder.txtMake = (TextView) v.findViewById(R.id.txt_make);
        holder.txtModel = (TextView) v.findViewById(R.id.txt_model);
        holder.c1 = (CheckBox) v.findViewById(R.id.c1);
        holder.c2 = (CheckBox) v.findViewById(R.id.c2);



        v.setTag(holder);
    }
    else {
        holder = (ViewHolder) v.getTag();
    }

    // fill row data
    Car currentItem = getItem(position);
    if(currentItem != null) {
        holder.txtMake.setText(currentItem.getMake());
        holder.txtModel.setText(currentItem.getModel());
        holder.c1.setText(currentItem.getc1());  //<--error here
        holder.c2.setText(currentItem.getc2());  //<--error here
    }

    return v;
}

As in Log:

Resources$NotFoundException: String resource ID #0x1

Probably getc1() and getc2() methods returning int type value so system considering both values as ids and trying to find in values resources.

Use String.valueOf method to show int value as text in CheckBox :

    holder.txtMake.setText(String.valueOf(currentItem.getMake()));
    holder.txtModel.setText(String.valueOf(currentItem.getModel()));
    holder.c1.setText(String.valueOf(currentItem.getc1()));  
    holder.c2.setText(String.valueOf(currentItem.getc2()));   

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