m_55
m_55

Reputation: 21

Accessing ListView CheckBox in Android

I'm making a to-do list app in android. Everything is working well enough except I have checkboxes in a ListView that I cannot set programmatically. I'm using a SQLite database that stores my tasks and whether or not they have been marked as complete. When I check a checkbox, it successfully saves a "1" in my database. I have an updateUI method that reads the text of my tasks from my database and displays it. I have made a new "updateUICheckBox" method to handle updating the UI for the checkboxes. Right now I'm just trying to set all my checkboxes as checked, but I'm getting a nullpointerexception when I call setChecked(true).

// not working
private void updateUICheckBox() {
    CheckBox c = (CheckBox) findViewById(R.id.list_complete_check_box);
    c.setChecked(true); // null pointer exception
}

Upvotes: 2

Views: 338

Answers (2)

kggoh
kggoh

Reputation: 742

Try add a flag in your list

public class Country {
    public String name;
    public String code;
    public String abbreviation;
    public boolean selected = false;   //--> example this flag
    public int image;
}

Adapter

public class CountryCodeAdapter extends BaseAdapter {
    private Context context;
    private List<Country> countryList;


    public CountryCodeAdapter(Context context, List<Country> countryList) {
        this.context = context;
        this.countryList = countryList;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_country_code, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Country country = countryList.get(position);
        viewHolder.tv_item_country_name.setText(country.getName());

        String plus = context.getResources().getString(R.string.plus);
        viewHolder.tv_item_country_code.setText(plus.concat(country.getCode()));
        int image = country.getImage();
        if (image != -1) {
            viewHolder.iv_item_country.setImageResource(image);
        }

        return convertView;
    }

    public void updateList(List<Country> countryList) {
        this.countryList = countryList;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.iv_item_country)
        ImageView iv_item_country;
        @Bind(R.id.tv_item_country_name)
        TextView tv_item_country_name;
        @Bind(R.id.tv_item_country_code)
        TextView tv_item_country_code;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

When you want to modify the flag, do this outside the adapter

CountryCodeAdapter adapter = new CountryCodeAdapter(activity, countryList);
//eg. you may change the 1st element in countryList selected flag into true and //call updateList 
        if (adapter != null) {
            adapter.updateList(countryList);
        }

Use this flag to set your checkbox inside the adapter.

Upvotes: 1

Samuel Robert
Samuel Robert

Reputation: 11082

Since it's a ListView item that you're setting up the CheckBox, you've to set it up in the adapter of the ListView itself. And manipulate the data model to check or uncheck the CheckBox.

Create new class by extending BaseAdapter for populating ListView. Do not use ArrayAdapter if you've custom layout with more than 1 UI object to manage

Upvotes: 1

Related Questions