how to update the textview text depending on the state of a checkbox in listview

空扰寡人 提交于 2019-11-29 08:49:15

istesd use

holder.ckbox.setText(checkedItems[position]?"Added","Add");

and remove

holder.tvckboxText.setText(ckboxTextAdd[position]?"Added","Add"););

this because it might not support the landscape view

Adil Soomro

Problem:

When you're updating the values of array to map the status of checked or un-checked, you're not updating the TextView, as CheckBox's check or uncheck works out-of-box as implemented the api, but TextView's text need to be updated.

Solution:

You need to either manually update the TextView's text, or simply call notifyDataSetChanged() whenever you check or uncheck the item, this will let the getView() of Adapter called and will force to refresh the row based on updated value.

Suggestion/Improvement:

  1. You can maintain single array of boolean only, no need to maintain the String array of Added or Add, while showing text, check if the boolean is true, set text as "Added", else "Add" like this:

    holder.ckbox.setChecked(checkedItems[position]);
    holder.tvckboxText.setText(checkedItems[position]?"Added":"Add");
    
  2. Instead of implementing anonymous CompoundButton.OnCheckedChangeListener, implement it to class level and set as this, as of now you're creating multiple CompoundButton.OnCheckedChangeListener object every time when you scroll through the list.

  3. Where are you closing the db? It doesn't matter though, but its good to have a closing point.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!