Note that there are some explanatory texts on larger screens.

plurals
  1. POPre-fill the CheckedTextViews in a ListView
    primarykey
    data
    text
    <p>Here is my first question on StackOverFlow, I usually always find an answer by myself but I am really stuck on a weird problem that I will explain here:</p> <p>I implemented a ListView in a fragment activity, this listview contains a list of categories related to the current record that I get from the SQLLite database.</p> <p>All is working fine, I created a SimpleCursorAdapter to retrieve the data from the DB and I display the categories correctly in the ListView. The problem is related to the pre-fill of the checkboxes (it is a multiselection list), depending on how I try to pre-check the checkboxes, I get 2 cases:</p> <p>First, the checkboxes are well pre-checked, but I cannot toggle the checkboxes anymore by clicking them. Second the click toggle well the checkboxes, but they are not pre-checked anymore...</p> <p>Here is the part of the code where I have the problem:</p> <pre><code>@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //super.onCreate(savedInstanceState); View v = inflater.inflate(R.layout.rate_fragment, container,false); dbCategories = ""; displayCategories = resources.getText(R.string.no_categories).toString(); /** INITIALIZATION */ mViewSwitcher = (ViewSwitcher)v.findViewById(R.id.profileSwitcher); /** Edition view */ rateGroup = (RadioGroup)v.findViewById(R.id.rate_group); rateOne = (RadioButton)v.findViewById(R.id.one_button); rateOne.setTag(1); rateTwo = (RadioButton)v.findViewById(R.id.two_button); rateTwo.setTag(2); rateThree = (RadioButton)v.findViewById(R.id.three_button); rateThree.setTag(3); rateFour = (RadioButton)v.findViewById(R.id.four_button); rateFour.setTag(4); rateFive = (RadioButton)v.findViewById(R.id.five_button); rateFive.setTag(5); descET = (EditText)v.findViewById(R.id.editdescription); descTextSize = descET.getTextSize(); descET.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); categoriesTV_edit = (TextView)v.findViewById(R.id.edit_categories); categoriesBT = (Button) v.findViewById(R.id.select_categories); categoriesBT.setOnClickListener(new OnClickListener() { public void onClick(View v) { View categoriesListTitle = getActivity().getLayoutInflater().inflate(R.layout.category_list_title, null); AlertDialog.Builder alt_bld = new AlertDialog.Builder(v.getContext()).setCustomTitle(categoriesListTitle); categories = db.getAllCategoriesByRate(currentRate); categoriesList = new ListView(getActivity()); categoriesList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); categoriesList.setClickable(true); String[] fromColumns = new String[] { DatabaseHandler.CATEGORY_NAME }; int[] toViews = new int[]{ R.id.cat_checked }; //mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_multiple_choice, categories, fromColumns, toViews, 0); mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.category_item, categories, fromColumns, toViews, 0); mAdapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (columnIndex == 1) { CheckedTextView categRow = (CheckedTextView) view; String catName = cursor.getString(1); mAdapter.setViewText((TextView) view, catName); int catChecked = cursor.getInt(2); //boolean checkedCat = catChecked==1; //categoriesList.setItemChecked(cursor.getPosition(),checkedCat); categRow.setChecked(catChecked==1); int catID = cursor.getInt(0); categRow.setTag(catID); return true; } else { return false; } } }); categoriesList.setAdapter(mAdapter); alt_bld.setView(categoriesList); </code></pre> <p>To have one case or another, all depends on these 2 lines:</p> <pre><code>//boolean checkedCat = catChecked==1; //categoriesList.setItemChecked(cursor.getPosition(),checkedCat); </code></pre> <p>If they are commented, the checkboxes are not pre-checked, but the toggle on the clicks is working. But if I comment these lines out, the toggle is not working anymore but the categories are prechecked.</p> <p>What I also don't understand is that this line is not working:</p> <pre><code> categRow.setChecked(catChecked==1); </code></pre> <p>But this one is working well (I succeed to retrieve the tag):</p> <pre><code> categRow.setTag(catID); </code></pre> <p>So I hope someone will succeed to explain to me what I do wrong, I guess there is something I misunderstood here...</p> <p><strong>NOTE</strong>: I get 3 columns from the cursor "categories", first one is the ID of the category, second one is the name, and third one is the status: checked or not (1 or 0).</p> <p>Thanks in advance for your time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload