Kevik
Kevik

Reputation: 9351

using viewHolder Interface to update state to Listview

in the code below in the else section that has getTag() inside of the ArrayAdapter

  holder = (ViewHolder) convertView.getTag();

how do I use this section to update or get the latest state of a view item and keep that state consistent with what the saved state is. like if you click a checkbox or get the checked state of the checkbox from the database and want that to be displayed in the correct state of the checkbox?

in my app i am getting the previous state of the checkbox from the database and setting the checkbox to that state when the Listview is created, and if the state of the box is changed then that change in state will be saved to the database and the checkbox will also show the change in state.

in other words what am i supposed to put in that section of code, besides the getTag line?

  if ((convertView == null)){

                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.smalltank_customer_row, null);

                holder = new ViewHolder();

                holder.textViewOne = (TextView) convertView.findViewById(R.id.textView1);
                holder.textViewTwo = (TextView) convertView.findViewById(R.id.textView2);
                holder.textViewThree = (TextView) convertView.findViewById(R.id.textView3);
                holder.radioGroupOne = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
                holder.radioButtonOne = (RadioButton) convertView.findViewById(R.id.radioButton1);
                holder.radioButtonTwo = (RadioButton) convertView.findViewById(R.id.radioButton2);
                holder.checkBoxOne = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.buttonOne = (Button) convertView.findViewById(R.id.button1);
                holder.buttonTwo = (Button) convertView.findViewById(R.id.button2);
                holder.buttonThree = (Button) convertView.findViewById(R.id.button3);
                holder.buttonFour = (Button) convertView.findViewById(R.id.button4);

                convertView.setTag(holder);

            }else{

                holder = (ViewHolder) convertView.getTag();

            }

Upvotes: 2

Views: 2186

Answers (2)

vs.thaakur
vs.thaakur

Reputation: 619

first create this class to hold the information of currently checked item

    public class Model {

      private String name;
      private boolean selected;

      public Model(String name) {
      this.name = name;
      selected = false;
    }

     public String getName() {
      return name;
   }

     public void setName(String name) {
      this.name = name;
    }

    public boolean isSelected() {
    return selected;
  }

    public void setSelected(boolean selected) {
    this.selected = selected;
  }

} 

and then in your getView()

    public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.rowbuttonlayout, null);
      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) view.findViewById(R.id.label);
      viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
      viewHolder.checkbox
          .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
              Model element = (Model) viewHolder.checkbox
                  .getTag();
              element.setSelected(buttonView.isChecked());

            }
          });
      view.setTag(viewHolder);
      viewHolder.checkbox.setTag(list.get(position));
    } else {
      view = convertView;
      ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    return view;
  }
} 

Upvotes: 0

Kai
Kai

Reputation: 15476

Since convertView can be recycled and is generally very volatile, by extension its viewHolder is equally volatile, so you shouldn't store any persistent data such as checkbox selections there. You should store your checkbox selection values somewhere that will be persisted when your Activity is paused and resumed.

As an example, I used a HashSet in one o my app to hold the contacts the user has selected which is saved and restored when the Activity is paused and resumed, sample code:

private HashSet<ContactData> selSet = new HashSet<ContactData>();

public View getView(int position, View convertView, ViewGroup parent) {
    if ((convertView == null)){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.smalltank_customer_row, null);

        holder = new ViewHolder();
        //init holder
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.status.setChecked(selSet.contains(data));
    return convertView;
}

public void onClick(View v) {
    UserHolder holder = (UserHolder) v.getTag();
    if (selSet.contains(holder.user)) {
        selSet.remove(holder.user);
    } else {
        selSet.add(holder.user);
    }
}

public final void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        selSet = (HashSet<ContactData>) savedInstanceState
                .getSerializable("selSet");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable("selSet", selSet);
}

Upvotes: 2

Related Questions