Android 在单击时将ListView更新为删除项
我正在使用一个带有ListView的游标适配器和一个从sqlite数据库获取数据的游标。我有一个名为RenderList()的函数,每当我用列表中的一个新项更新数据库时,或者如果我将一行的选中值设置为1(这将添加新项或删除项名称),我都会调用该函数 这将重置列表,因此,如果单击listview下方的项目,它将将listview重置为顶部位置。很明显,我缺少了一些关于如何以高效、可用的方式更新listview和数据库的信息Android 在单击时将ListView更新为删除项,android,listview,adapter,Android,Listview,Adapter,我正在使用一个带有ListView的游标适配器和一个从sqlite数据库获取数据的游标。我有一个名为RenderList()的函数,每当我用列表中的一个新项更新数据库时,或者如果我将一行的选中值设置为1(这将添加新项或删除项名称),我都会调用该函数 这将重置列表,因此,如果单击listview下方的项目,它将将listview重置为顶部位置。很明显,我缺少了一些关于如何以高效、可用的方式更新listview和数据库的信息 public class GroceryAdapter extends C
public class GroceryAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
public GroceryAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
// mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TwoLineListItem listItem = (TwoLineListItem)view;
TextView t1 = listItem.getText1();
TextView t2 = listItem.getText2();
t1.setText(cursor.getString(cursor.getColumnIndex(DbHelper.C_GROCERY)));
t2.setText("Added by: Wes");
t1.setTag(cursor.getInt(cursor.getColumnIndex(DbHelper.C_ID)));
t2.setTag(cursor.getInt(cursor.getColumnIndex(DbHelper.C_CHECKED)));
if (cursor.getInt(cursor.getColumnIndex(DbHelper.C_CHECKED)) == 1 ) {
t1.setPaintFlags(t1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
listItem.setBackgroundColor(0xEECCCCCC);
} else {
t1.setPaintFlags(t1.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG) );
listItem.setBackgroundColor(0x00000000);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.grocery_list_item, parent, false);
return view;
}
}
您可以使用adapter.notifydatasetchangedLike Iftikar said-notifyDataSetChanged。这会告诉适配器数据已更改,并导致listview完全刷新自身。我已尝试notifyDataSetChanged,但它似乎没有更新视图。我是否需要在游标适配器中添加某种侦听器/观察者方法?
public class GroceryAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
public GroceryAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
// mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TwoLineListItem listItem = (TwoLineListItem)view;
TextView t1 = listItem.getText1();
TextView t2 = listItem.getText2();
t1.setText(cursor.getString(cursor.getColumnIndex(DbHelper.C_GROCERY)));
t2.setText("Added by: Wes");
t1.setTag(cursor.getInt(cursor.getColumnIndex(DbHelper.C_ID)));
t2.setTag(cursor.getInt(cursor.getColumnIndex(DbHelper.C_CHECKED)));
if (cursor.getInt(cursor.getColumnIndex(DbHelper.C_CHECKED)) == 1 ) {
t1.setPaintFlags(t1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
listItem.setBackgroundColor(0xEECCCCCC);
} else {
t1.setPaintFlags(t1.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG) );
listItem.setBackgroundColor(0x00000000);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.grocery_list_item, parent, false);
return view;
}
}