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;
}