-1

I'm using SimpleAdapter to populate ListView and every row has two buttons TextView and RadioGroup buttons. As you can see in the code when I click button A, I set button A to be highlighted, set TextView to 1 and set text radio button A to 1 and vice versa when I click button B. But all the values disappear when I scroll down. One of the answers I have followed : How ListView's recycling mechanism works without success.

@Override
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
System.out.println("getView " + position + " " + v);
ViewHolder holder = null;

if(v == null){
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.lv_test, null);

    holder = new ViewHolder();

    holder.btnA = (Button) v.findViewById(R.id.btn_a);
    holder.btnB = (Button) v.findViewById(R.id.btn_b);
    holder.tvPick = (TextView) v.findViewById(R.id.pick);

    holder.radioPickGroup = (RadioGroup)v.findViewById(R.id.rg);
    holder.rb_a = (RadioButton)v.findViewById(R.id.rb_a);
    holder.rb_b = (RadioButton)v.findViewById(R.id.rb_b);

    v.setTag(holder);

} else {
    holder = (ViewHolder)v.getTag();
}

holder.btnA.setText(pickList.get(position).get(TAG_A));
holder.btnA.setTypeface(OpenSans);

holder.btnB.setText(pickList.get(position).get(TAG_B));
holder.btnB.setTypeface(OpenSans);

holder.tvPick.setText(pickList.get(position).get(TAG_PICK));
holder.tvPick.setTypeface(OpenSans);


final String thePick = holder.tvPick.getText().toString();
        if (thePick.equals("1")){
            holder.btnA.setBackgroundResource(R.drawable.btn1_highlight);
            holder.rb_a.setChecked(true);
            holder.rb_b.setChecked(false);
        } else if (thePick.equals("2")){
            holder.btnB.setBackgroundResource(R.drawable.btn1_highlight);
            holder.rb_b.setChecked(true);
            holder.rb_a.setChecked(false);
        } else if (thePick.equals("0")){
            holder.btnA.setBackgroundResource(R.drawable.btn2);
            holder.btnB.setBackgroundResource(R.drawable.btn2);
            holder.rb_b.setChecked(false);
            holder.rb_a.setChecked(false);
        }

final ViewHolder finalHolder1 = holder;
holder.btnB.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        finalHolder1.tvPick.setText("2");

        finalHolder1.btnA.setBackgroundResource(R.drawable.btn2);
        finalHolder1.btnB.setBackgroundResource(R.drawable.btn1_highlight);

        finalHolder1.rb_b.setChecked(true);
        finalHolder1.rb_a.setChecked(false);

        int selectedId = finalHolder1.radioPickGroup.getCheckedRadioButtonId();

        final RadioButton radioMyPick = (RadioButton) findViewById(selectedId);
        String pick_string = radioMyPick.getText().toString();

        /*Toast.makeText(getApplicationContext(),
                pick_string, Toast.LENGTH_SHORT).show();*/
    }
});

holder.btnA.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        finalHolder1.tvPick.setText("1");
        finalHolder1.btnA.setBackgroundResource(R.drawable.btn1_highlight);
        finalHolder1.btnB.setBackgroundResource(R.drawable.btn2);

        finalHolder1.rb_b.setChecked(false);
        finalHolder1.rb_a.setChecked(true);

        int selectedId = finalHolder1.radioPickGroup.getCheckedRadioButtonId();

        final RadioButton radioMyPick = (RadioButton) findViewById(selectedId);
        String pick_string = radioMyPick.getText().toString();

        /*Toast.makeText(getApplicationContext(),
                pick_string, Toast.LENGTH_SHORT).show();*/
    }
});

return v;
}


public static class ViewHolder {
    TextView tvPick;
    RadioGroup radioPickGroup;
    RadioButton rb_a, rb_b;
    Button btnA, btnB;
}
Community
  • 1
  • 1
forel
  • 33
  • 6

1 Answers1

1

The problem here is that you are not storing the state of your views anywhere except the views themselves. Because views are reused it is not sufficient to only store the state in the views as this will get overwritten when the views are reused. It looks like pickList is your data source, and all the views are set from the initial state of pickList, but you never update pickList when the various buttons are pressed. When the buttons are pressed you need to store the new state of your views in your data source as well as updating the views themselves.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • By updating `pickList` you mean like this `holder.tvPick.setTag(pickList.get(position));` ?? And when I moved `holder.tvPick.setText(pickList.get(position).get(TAG_PICK));` to the `if(v == null)` values don't disappear but are repeated. If I have 4 visible rows on start, on 5th row I have repeated values and so on. I also set tags in `else` of `if(v == null)` but I'm not sure if that is even necessary. I'm confused where to set and get tags. [Pastebin](http://pastebin.com/q181dyBn) to the repeated values. – forel Aug 27 '14 at 03:10
  • No, you need to store the updated values in `pickList`. It's difficult to be specific as you haven't included the code which tells us what `pickList` is, but in your `holder.btnB.setOnClickListener()` I'd expect it to look something more like `pickList.get(position).put(TAG_PICK, "2");` I've assumed here that `pickList` is some sort of `List` of `Map`s, but if my assumption is wrong, you might need to modify it. – HexAndBugs Aug 27 '14 at 08:46