我正在尝试从我的ListView中删除长按项目,由游标适配器填充,项目被删除但不通知适配器。
我读过这个:
How to notifyDataSetChange() from onitemclick() method?
并且该解决方案可能适用于ArrayAdapter的子类,但是我的扩展了CursorAdapter,
cant resolve method: notifyDataSetChanged()(尝试使用该问题的答案解决)
notifyDataSetChanged()中的代码:
notesListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemLongClick: " + position + " id: 0" + id);
List<String> taskList = dbhelper.getTaskList(userName);
Log.d(TAG, "onItemLongClick: " + taskList);
dbhelper.deleteTask(userName, taskList.get(position));
taskList.remove(position);
cursorAdapter.notifyDataSetChanged();
// ListAdapter adapter = notesListView.getAdapter();
// adapter.notifyDataSetChanged(); can't resolve method
另一件事是,每次用户长按某个项目时,都会从数据库中检索List<String>
,并将其从按下的position
中删除。
我做错了什么?我还需要一个很好的方法来从数据库中删除项目,这是事先感谢!
修改:
以下是我的适配器的代码:
public class MyCursorAdapter extends CursorAdapter {
private LayoutInflater inflater;
public MyCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item_cursor, parent, false);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView titleTextView = (TextView) view.findViewById(R.id.list_title);
TextView detailsTextView = (TextView) view.findViewById(R.id.list_details);
String titleString = null;
String detailsString = null;
try {
titleString = cursor.getString(1);
detailsString = cursor.getString(2);
} catch (Exception e) {
Log.d("TAG", "Exception on: " + e.toString());
}
titleTextView.setText(titleString);
detailsTextView.setText(detailsString);
}
}