ListView值消失

时间:2014-08-26 02:43:43

标签: android listview android-viewholder

我正在使用SimpleAdapter填充ListView,每行都有两个按钮TextView和RadioGroup按钮。正如您在单击按钮A时在代码中看到的那样,我将按钮A设置为高亮显示,将TextView设置为1并将文本单选按钮A设置为1,反之亦然,当我单击按钮B时。但滚动时所有值都消失了下。我遵循的答案之一:How ListView's recycling mechanism works没有成功。

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

1 个答案:

答案 0 :(得分:1)

这里的问题是除了视图本身之外,您不会将视图的状态存储在任何位置。因为视图被重用,所以仅在视图中存储状态是不够的,因为在重用视图时这将被覆盖。看起来pickList是您的数据源,并且所有视图都是从pickList的初始状态设置的,但是在按下各种按钮时您永远不会更新pickList。按下按钮后,您需要将新视图状态存储在数据源中,并自行更新视图。