Android Listview项目重复

时间:2015-05-22 06:48:24

标签: android listview

每次页面加载时,我的listview项都会继续重复。我认为adapter课程中存在错误。请帮忙。提前谢谢。

public class MyAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;

    public MyAdapter(Activity context, List<Model> list) {
        super(context, R.layout.rows, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.rows, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });
            convertView.setTag(viewHolder);
            //convertView.setTag(R.id.label, viewHolder.text);
            //convertView.setTag(R.id.check, viewHolder.checkbox);
            } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.checkbox.setTag(position); // This line is important.

        viewHolder.text.setText(list.get(position).getName());
        viewHolder.checkbox.setChecked(list.get(position).isSelected());

        return convertView;
    }
}

我用这个函数填充列表,我在片段中调用了onCreate方法

private List getModel(){

    list.clear();
    for(int i=0;i<list1.size();i++){
        String a=list1.get(i);
        list.add(new Model(a));
    }
    return list;
}

这是我的片段类

public class VaccAdmin extends FragmentActivity implements   OnItemClickListener{
private Database dbHelper;    
private double x,y;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
List<String> list1=new ArrayList<String>();

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.list);
    dbHelper=new Database(this);
    dbHelper.addVaccine();
    listView = (ListView) findViewById(R.id.my_list);

    String age = getIntent().getStringExtra("age");
    System.out.println(age+"Ageinmo");
    double ageinmo=Double.parseDouble(age);
    if(ageinmo<=1.5){
        x=0.0;
        y=1.5;
    } else if(ageinmo<=2.25){
        x=1.75;
        y=2.25;
    } else if(ageinmo<=4.25){
        x=3.5;
        y=4.25;
    }else if(ageinmo<=6.0){
        x=4.5;
        y=6.0;
    }else if(ageinmo<=9.0){
        x=6.25;
        y=9.0;
    }else if(ageinmo<=12.0){
        x=9.25;
        y=12.0;
    }else if(ageinmo<=15.0){
        x=12.25;
        y=15.0;
    }else if(ageinmo<=18.0){
        x=15.25;
        y=18.0;
    }else if(ageinmo<=23.0){
        x=18.25;
        y=23.0;
    }else if(ageinmo<=72.0){
        x=36.25;
        y=72.0;
    }else if(ageinmo<=120.0){
        x=72.25;
        y=120.0;
    }else if(ageinmo<=144.0){
        x=120.25;
        y=144.0;
    }else if(ageinmo<=216.0){
        x=144.25;
        y=216.0;
    }
    list1=dbHelper.getVacNames(x, y);
    int size=list1.size();
    System.out.println("Size"+size);
    adapter = new MyAdapter(this,getModel());
    listView.setAdapter(null);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
            TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString()+"   "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}

private String isCheckedOrNot(CheckBox checkbox) {
    if(checkbox.isChecked())
    return "is checked";
    else
    return "is not checked";
}


private List<Model> getModel() {

    list.clear();
    for(int i=0;i<list1.size();i++){
        String a=list1.get(i);
        list.add(new Model(a));
    }
    return list;
}

}

1 个答案:

答案 0 :(得分:0)

我相信这应该写成outide convertview == null

 viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                    list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                }
            });