适配器删除不起作用

时间:2015-06-08 19:13:16

标签: android sqlite

我尝试从自定义适配器中删除该项,并通知数据已更改。但这似乎不起作用。

mListDataAdapter.remove(mListDataAdapter.getItem(position));
mListDataAdapter.notifyDataSetChanged();

两者都不起作用mListDataAdapter.clear();在调试模式下没有对适配器进行任何更改。

如果有帮助,我的活动会延长AppCompatActivity。我在onActionItemClicked内调用此代码。同时我从数据库调用delete,这是正确的。

更新。 这是ListDataAdapter:

public class ListDataAdapter extends ArrayAdapter {

List list = new ArrayList();


public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}

static class LayoutHandler{
TextView id_;
TextView title;

}


@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

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

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

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

    View row = convertView;
    LayoutHandler layoutHandler;

    if(row == null){

        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.todo_row_layout,parent,false);

        layoutHandler = new LayoutHandler();
        layoutHandler.id_ = (TextView) row.findViewById(R.id.id_);
        layoutHandler.title = (TextView) row.findViewById(R.id.todoTitle);
        row.setTag(layoutHandler);
    } else {
        layoutHandler = (LayoutHandler) row.getTag();

    }

        DataProvider dataProvider = (DataProvider) this.getItem(position);
        layoutHandler.title.setText(dataProvider.getTitle());
        layoutHandler.id_.setText(String.valueOf(dataProvider.getId_()));

    return row;

   // return super.getView(position, convertView, parent);
}

}

3 个答案:

答案 0 :(得分:0)

在CustomAdapter类中使用以下删除函数:将mList视为CustomAdapter中数据对象的ArrayList。

HTTP/1.1 504 Fiddler - Receive Failure"

[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes. 

答案 1 :(得分:0)

当您remove/add/change数据时,您应该在adapter中使用数据模型,然后在notified中使用adapter。在你的情况下,我建议如下:

adapter课程中,创建一种删除数据的方法:

public void remove(Object obj) { this.list.remove(obj); notifyDataSetChanged(); //by call this you should not to call adapter.notifyDataSetChange() }

答案 2 :(得分:0)

在适配器中,定义删除功能

public void remove(int position)
{
    list.remove(position);
    this.notifyDataSetChanged();
}
public void remove(Object object)
{
    list.remove(object);
    this.notifyDataSetChanged();
}

如果要删除一次,请调用mListDataAdapter.remove(position)或mListDataAdapter.remove(object)