始终从db中删除最后一项

时间:2014-04-23 05:58:57

标签: android database list listview

我在这里遇到了一些问题。在选择要删除的列表视图项时,其工作正常。但是从db重新加载列表时,db中删除的列表始终是最后一个列表。并且再次出现项目选择。

这里是代码:

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.search_results_item_remove_view, null);

    list = data.get(position);

    TextView title = (TextView) vi.findViewById(R.id.item_text); // title
    ImageView remove = (ImageView) vi.findViewById(R.id.item_remove);
    remove.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            db.deleteContact(list.getID());
            data.remove(position);
            notifyDataSetChanged();
            Toast.makeText(activity, "Please wait. updating list...",
                  Toast.LENGTH_LONG).show();
        }
    });        

    // Setting all values in listview
    title.setText(list.getName().toString());
    return vi;
}

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

使用项目OnClickListener:

public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
        public void onClick(View v) {
            final int position = list_v
                    .getPositionForView((View) v.getParent());
            list = data.get(position);
            db.deleteContact(list.getID());
            data.remove(position);
            notifyDataSetChanged();

            Log.d("you are click on Ratings","you are click on Ratings");

        }
    };

here is full example