Question

I have an adapter that uses a CheckedTextView and extends from a SimpleCursorAdapter. I tried to load the default values for CheckTextView but I didn't manage to do it. I don't know if this it's the behavior of CheckedTextView or I am doing something wrong.

What I have tried?

I tried to override the getView() method and get the CheckedTextView view and use setChecked(boolean), FAILED, weird that it doesn't work.

Then I tried to use a ViewBinder, but that doesn't work either.

Any insight to this problem?

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                CheckedTextView cb = (CheckedTextView) view;
                int checked=cursor.getInt(cursor.getColumnIndex(ProgramSurvey.CHECKED));
                cb.setChecked(checked==1);

                return false;
            }
        });
Was it helpful?

Solution

If the View returned from getView is a CheckedTextView (or any other view that implements Checkable), then the listView will set the checked on the relevant rows, based on which rows it thinks are selected (from calls to setItemChecked). You'll need to tell the ListView which rows are checked, rather than trying to set it yourself, or alternatively wrap your view in a view which doesn't implement checkable, then you'll be able to set checked yourself in the CheckedTextView.

OTHER TIPS

You can try this:

listview.setItemChecked(position,true);

It actually helped the idea from @superfell, but since my binder depends on a Cursor, I had to requery on the clicked even from the ListView, like this

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterlist, View view,
                    int pos, long id) {

                CheckedTextView checkBox = (CheckedTextView) view
                        .findViewById(R.id.checkTextProgram);
                CursorWrapper cursor = (CursorWrapper) adapterlist
                        .getItemAtPosition(pos);

                if (checkBox.isChecked()) {
                    String programId = cursor.getString(cursor
                            .getColumnIndex(Program._ID));
                    getActivity().getContentResolver().delete(
                            ProgramSurvey.buildUri(_ID),
                            ProgramSurvey.PROGRAM_ID + "=?",
                            new String[] { programId });
                } else {
                    ContentValues values = new ContentValues();
                    values.put(ProgramSurvey.PROGRAM_ID, cursor
                            .getString(cursor.getColumnIndex(Program._ID)));
                    values.put(ProgramSurvey.SURVEY_ID, _ID);
                    getActivity().getContentResolver().insert(
                            ProgramSurvey.CONTENT_URI, values);
                }

                getLoaderManager().restartLoader(0, null, ProgramDialogFragment.this);

            }

        });

So I used the loader manager to requery the cursor on demand.

I just tried to inflate layout (android.R.layout.simple_list_item_multiple_choice)...It gives simply modified checkbox images and textview colors and padding.It is similar to checkedTextview.

Adapter class:

public class CustomListAdapter extends BaseAdapter {

    private String[] stringArray;
    private Context mContext;
    private LayoutInflater inflator;
    int checkbox;
    /**
     * 
     * @param context
     * @param stringArray
     */
    public CustomListAdapter(Context  context, String[] stringArray) 
    {
        this.mContext=context;
        this.stringArray=stringArray;
        this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount()
    {
        return stringArray.length;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        final MainListHolder mHolder;
        View v = convertView;
        if (convertView == null)
        {
            mHolder = new MainListHolder();
            v = inflator.inflate(android.R.layout.simple_list_item_multiple_choice, null);
            mHolder.txt=(CheckedTextView) v.findViewById(android.R.id.text1);
            v.setTag(mHolder);
        } 
        else
        {
            mHolder = (MainListHolder) v.getTag();
        }
        mHolder.txt.setText(stringArray[position]);
        mHolder.txt.setTextSize(12);
        mHolder.txt.setTextColor(Color.YELLOW);

    /**
     * When checkbox image is set By setImageFromResourceCheckBox(int id) method...Otherwise it takes default
     */
        if(checkbox!=0)
            mHolder.txt.setCheckMarkDrawable(R.drawable.android_button);

        mHolder.txt.setPadding(5, 5, 5, 5);
        return v;
    }
    class MainListHolder 
    {
        private CheckedTextView txt;

    }
    /***
     * Setting Image for Checkbox
     * @param id
     * 
     */
    public void setImageFromResourceCheckBox(int id)
    {
        this.checkbox=id;
    }


}

Main Activity:

public class CheckedListActivity extends ListActivity implements OnItemClickListener  {

    //final String[] str={"Android","Black Berry","Iphone","Ipad"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CustomListAdapter c=new CustomListAdapter(this,GENRES);
        c.setImageFromResourceCheckBox(R.drawable.android_button);
        setListAdapter(c);

        final ListView listView = getListView();
        listView.setItemsCanFocus(false);
        //listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// For single mOde

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        //used for default selection .....
        listView.setItemChecked(2, true);
        listView.setOnItemClickListener(this);
    }


    private static final String[] GENRES = new String[] 
                                                {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };
    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
    {

    SparseBooleanArray sp=getListView().getCheckedItemPositions();

    String str="";
    for(int i=0;i<sp.size();i++)
    {
        str+=GENRES[sp.keyAt(i)]+",";
    }
    Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top