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

前端 未结 2 1977
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 16:28

Here is My Custom ListViewAdapter

public class ListViewAdapter extends BaseAdapter{


    Viewholder holder;
    public ArrayList>         


        
相关标签:
2条回答
  • 2020-12-18 16:56

    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

    0 讨论(0)
  • 2020-12-18 16:58

    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.

    0 讨论(0)
提交回复
热议问题