vicky vicky - 1 year ago 127
Android Question

Android listView with CheckedTextView, populate with few checked rows

I am new to android,I am trying to show a listview with CheckedTextView on screen with few elements already checked, But couldn't succeed, my listview is always unchecked
Here is my code,

final ListView listView = (ListView) findViewById(;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item,, routesSubList);
for (int i = 0; i < routesSubList.size(); i++) {
if (selectedRoutesList.contains(routesSubList.get(i))) {
//listView.setItemChecked(i,true); No Luck
View v = listView.getAdapter().getView(i, null, null);
CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(;

checkedTextView.setChecked(true);//I get correct row to be checked but not shown on screen

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(;
boolean checked = checkedTextView.isChecked();
// String str =
if (checked) {
} else {

Please advice, I dont know if i am missing something

Answer Source

You have given the listView a custom layout, so it don't know how it should be looked when an item is checked. You can try with override the getView().

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item,, routesSubList){
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = getActivity().getLayoutInflater().inflate(R.layout.routes_list_item);
            CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(;
            return convertView;

The code is not tested. Hope it help!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download