滚动时清除ListView数据

时间:2015-03-25 14:00:16

标签: android listview android-listview

我正在处理一个显示listView checkBoxes的应用程序。 当我滚动listView时,CheckBoxes的值已清除。

以下是我的代码

适配器类:

public class Conference_listViewAdapter extends BaseAdapter {
    Activity context;
    LayoutInflater inflater;
    public static List<Model> data = null;
    ArrayList<String> id_list = new ArrayList<String>();
    ViewHolder holder;
    int pos;

    public Conference_listViewAdapter(Activity context,List<Model> list, ArrayList<String> id_list) {
        data = list;
        this.id_list = id_list;     
        this.context = context;
    }


    @Override
    public int getCount() {
        return data.size();
    }


    @Override
    public Object getItem(int position) {
        return data.get(position).getName();
    }

    @Override
    public long getItemId(int position) {
        return Long.valueOf(id_list.get(position));
    }

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

        View view = null;

        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.people_list_item1, null);
            holder = new ViewHolder();
            holder.title = (TextView) view.findViewById(R.id.tv_name);
            holder.image = (ImageView) view.findViewById(R.id.avtaar);
            holder.favorite = (CheckBox) view.findViewById(R.id.fav);
            holder.favorite.setTag(position);
            view.setTag(holder);

            holder.favorite.setTag(data.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).favorite.setTag(data.get(position));
        }


        holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    
                Model element = (Model) holder.favorite.getTag();
                element.setSelected(buttonView.isChecked());     
                //notifyDataSetChanged();
            }
        });


        ViewHolder holder = (ViewHolder) view.getTag();

        holder.favorite.setChecked(data.get(position).isSelected());
        holder.title.setText(data.get(position).getName());



        try {
            holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    class ViewHolder {
        TextView title;
        TextView model;
        ImageView image;
        CheckBox favorite;
    }


}

数据模型类

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

} 

不知道我做错了什么。

4 个答案:

答案 0 :(得分:2)

您必须保存ViewHolder的状态。然后在tag时设置convertView == null并将其设置为else条件。

检查我的方式:

public class Conference_listViewAdapter extends ArrayAdapter<Model> {
    Activity context;
    LayoutInflater inflater;
    public static List<Model> data = null;
    ArrayList<String> id_list = new ArrayList<String>();

    int pos;


    public Conference_listViewAdapter(Activity context, List<Model> list, ArrayList<String> id_list) {
        super(context, R.layout.people_list_item1, list);
        this.context = context;
        this.data = list;
        this.id_list = id_list; 
    }



    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;       
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.people_list_item1, null);
            final ViewHolder holder = new ViewHolder();
            holder.title = (TextView) view.findViewById(R.id.tv_name);
            holder.image = (ImageView) view.findViewById(R.id.avtaar);
            holder.favorite = (CheckBox) view.findViewById(R.id.fav);

            view.setTag(holder);

            holder.favorite.setTag(data.get(position));

            holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    
                    Model element = (Model) holder.favorite.getTag();
                    element.setSelected(buttonView.isChecked());     
                }
            });


        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).favorite.setTag(data.get(position));
        }





        ViewHolder holder = (ViewHolder) view.getTag();     
        holder.favorite.setChecked(data.get(position).isSelected());
        holder.title.setText(data.get(position).getName());

        try {
            holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }




    static class ViewHolder {
        protected TextView title;
        protected CheckBox favorite;
        ImageView image;
    }

答案 1 :(得分:0)

改变这个..

  @Override
public Object getItem(int position) {
    return position;
}

要..

  @Override
public Object getItem(int position) {
    return data.get(position);
}

更新

试试这个..

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

    View view = null;

    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.people_list_item1, null);
        holder = new ViewHolder();
        holder.title = (TextView) view.findViewById(R.id.tv_name);
        holder.image = (ImageView) view.findViewById(R.id.avtaar);
        holder.favorite = (CheckBox) view.findViewById(R.id.fav);
        holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    
               //do what you want..   
            }
        });

        view.setTag(holder);
    } else {
       holder = (ViewHolder) view.getTag();
    }

    holder.favorite.setChecked(data.get(position).isSelected());
    holder.title.setText(data.get(position).getName());



    try {
        holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return view;
}

答案 2 :(得分:0)

在类似情况下,例如带有复选框,单选按钮,计数框等的列表视图,我用来做如下操作 1)保持一个大小与listview相似的arraylist(行数) 2)此数组/ arraylist将在适配器类的构造函数中初始化,所有位置均为零 3)在声明复选框并将其标记设置为位置

之后,在getView()内部
public class Conference_listViewAdapter extends BaseAdapter {
    Activity context;
    LayoutInflater inflater;
    public static List<Model> data = null;
    ArrayList<String> id_list = new ArrayList<String>();
     ArrayList<String> myarray=new java.util.ArrayList<String>();   
    ViewHolder holder;
    int pos;


    public Conference_listViewAdapter(Activity context,List<Model> list, ArrayList<String> id_list) {
        data = list;
        this.id_list = id_list;
        this.context = context;
for(int i=0;i<id_list.size();i++)
myarray.add("0");
    }


    @Override
    public int getCount() {
        return data.size();
    }


    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        View view = null;

        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.people_list_item1, null);
            holder = new ViewHolder();
            holder.title = (TextView) view.findViewById(R.id.tv_name);
            holder.image = (ImageView) view.findViewById(R.id.avtaar);
            holder.favorite = (CheckBox) view.findViewById(R.id.fav);
            holder.favorite.setTag(position);
            view.setTag(holder);

if(myarray.get(Integer.parseInt(holder.favorite.getTag().toString()).equalsIgnoreCase("1")
//make your checkbox checked
else
//keep checkbox unchecked
            holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    
   if(  isChecked)                   myarray.set(Integer.parseInt(holder.favorite.getTag().toString()),"1");    

else
myarray.set(Integer.parseInt(holder.favorite.getTag().toString()),"0"); 

notifydatasetchanged();
            });

            holder.favorite.setTag(data.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).favorite.setTag(data.get(position));
        }


        ViewHolder holder = (ViewHolder) view.getTag();

        holder.favorite.setChecked(data.get(position).isSelected());
        holder.title.setText(data.get(position).getName());



        try {
            holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    class ViewHolder {
        TextView title;
        TextView model;
        ImageView image;
        CheckBox favorite;
        Boolean selected;
    }   

}

这总是可以通过listview滚动保存我的问题..

答案 3 :(得分:0)

我认为这是一个常见的错误。您需要设置此代码 if / else块之外的holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener()。原因是 convertView是否= null ,应该设置监听器。我意识到你的代码可能需要一些安排,但我认为你可以做得比我好,因为我的代码风格不同。这是否足够清楚了?

另一个可能的错误是holder.favorite.setTag(position)。原因是您的侦听器中的代码Model element = (Model) holder.favorite.getTag()。您正在保存int position并将其类型转换为Model类,无法正常工作。更改覆盖 getItem ,首先进行编码。摘要中的示例代码:

@Override
    public Object getItem(int position) {
        return data.get(position);   // return the Model
    }
...
holder.favorite.setTag( getItem(position) );

注意:再次 holder.favorite.setTag 应设置为外部 if / else块。只能在if块中设置view.setTag(holder),因为它是一个新的视图对象。

除此之外,我认为您的代码设计应该有效!

  

建议的代码更改......

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

ViewHolder holder = null;    // different

if (convertView == null) {
   ...
   holder.favorite.setTag(data.get(position));    // different
   view.setTag(holder);                           // different

}
else {
   view = convertView;
   holder = (ViewHolder) view.getTag();    // different
}
...
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   Model element = (Model) buttonView.getTag();    // different

变更是:

  1. 我使用不同的注释来强调代码更改。
  2. ViewHolder holdergetView()中声明。删除课程顶部的持有人声明。我认为这是更好的编码风格,因为持有人更接近于找到你使用它的地方,使其成为getView本地。
  3. 切换holder.favorite.setTagview.setTag的展示位置。我认为这是一种更安全的编码风格。我认为在将持有者用作对象视图的参数之前先设置持有者会更安全。我承认,这可能无关紧要。
  4. convertView不是= null 时,
  5. view.getTag是必需的。当您在同一个区块中使用view.getTag()).favorite.setTag设置代码时,您实际上会以意外行为更改持有者的内容。
  6. 在代码(Model) buttonView.getTag()中,使用buttonView代替holder.favorite。我希望这能得到正确的Model对象。