调用notifyDataSetChanged()后listview没有正确刷新

时间:2015-09-30 07:00:44

标签: android listview android-listview

在从数据库中删除项目后,我遇到了刷新listview的问题。

问题是,当我调用notifyDataSetChanged()时,它返回一个listView,每次从列表中删除最后一个索引元素而不是特定索引(例如,我想删除第二个索引项后删除项listView显示每次删除最后一项,但实际列表和数据库包含正确的值)。

我无法理解这个问题,为什么notifyDataSetChanged()方法表现得像这个,解决方案是什么。

BaseAdapter类中的代码:

public class CustomReminderAdapter extends BaseAdapter {

    private Context context;
    private List<ReminderData> datalist;
    Viewholder holder;
    private AddReminderHelperDao dao;


    public CustomReminderAdapter(Context context,
            List<ReminderData> datalist) {
        super();
        this.context = context;
        this.datalist = datalist;

    }

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

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

    @Override
    public long getItemId(int position) {
        return datalist.get(position).getId();
    }

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

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate( R.layout.custom_list_item, null);
        holder = new Viewholder();
        holder.delete = (LinearLayout) convertView .findViewById(R.id.deletelayout);

        holder.delete.setTag(position);

        convertView.setTag(holder);
    } else {
        holder = (Viewholder) convertView.getTag();
    }

    holder.delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // datalist contain all values from database
            int positionToRemove = (int)v.getTag();
            int id= datalist.get(positionToRemove).getId();

            try {
                // dao method call for delete from database
                dao.deleteReminder(id);
                datalist.remove(positionToRemove);
                notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                dao.close();
            }

        }
    });
}

}

ViewHolder类:

private class Viewholder {
    private TextView titleTextView, timeDetail;
    private ImageView remindertypeImageView,conformimage;
    private ImageView sendwish;
    private LinearLayout topsideLayout, bglayout, bottomsideLayout,
            bottomrightstrip, topLayout, editLayout, snoozedlayout;
    private LinearLayout delete, conform, edit;
}

请帮帮我。

1 个答案:

答案 0 :(得分:1)

在不知道您的完整类文件的情况下进行猜测非常困难。但是,您绝对应该遵循视图符号模式并更改您的getview方法,如下所示:

convertView = mInflater.inflate( R.layout.custom_list_item, null);

更改

 convertView = mInflater.inflate( R.layout.custom_list_item, parent,false);

getItemId()

另外,请将 @Override public long getItemId(int position) { return position; } 更新为

holder.delete.setOnClickListener(new View.OnClickListener() {
        @Override 
        public void onClick(View v) {
            // datalist contain all values from database 
            int positionToRemove = (int)v.getTag();
            int id= datalist.get(positionToRemove).getId();

            try { 
                // dao method call for delete from database 
                dao.deleteReminder(id);
                // Directly pass the object instead of passing any positions
                datalist.remove(datalist.get(positionToRemove));
                notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            } finally { 
                dao.close();
            } 

        } 
    }); 

修改:

ELSE