ITMonkeyKing
换元不配限
2017-03-22 13:09

AndroidSQLite数据库删除数据后ListView的更新

12
  • adapter
  • sqlite
  • list

一款记账的软件,我的数据存储在SQLite中,当我长按item时,弹出对话框并确定删除该item,但是数据库中的删掉了,activity中的listView中该item还在,依理来说是因为我的adapter适配器加载的还是内存中的数据,但是小白还是没有想到好的解决办法,我试了使用广播,但是出现整个页面的item都被删了(数据库的其他还在)。其他的各种办法也都试了还是不行,无奈求助各位看到帖子的大神。讲解的好,我绝对给高分。先谢过各位了。

图片说明

适配器代码在此:

 public class CostListAdapter extends BaseAdapter {
    private List<CostBean> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public CostListAdapter(Context context, final List<CostBean> list){
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.zsf.accountbook.MY_BROADCAST");
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mList.clear();
                mList = list;
                notifyDataSetChanged();
            }
        },intentFilter);
    }


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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_item,null);
            holder.mTvCostCategory = (TextView) convertView.findViewById(R.id.tv_category);
            holder.mTvCostDate = (TextView) convertView.findViewById(R.id.tv_date);
            holder.mTvCostMoney = (TextView) convertView.findViewById(R.id.tv_money);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        CostBean costBean = mList.get(position);//
        holder.mTvCostCategory.setText(costBean.costCategory);
        holder.mTvCostDate.setText(costBean.costDate);
        holder.mTvCostMoney.setText(costBean.costMoney);

        return convertView;
    }


    private static class ViewHolder{
        public TextView mTvCostCategory;
        public TextView mTvCostDate;
        public TextView mTvCostMoney;
    }
}

图片说明

图片说明

漏掉了数据库部分的代码:
图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

8条回答

为你推荐

换一换