在android中我有一个自定义适配器,在这个自定义适配器中,我创建了一个视图,其中包含一些文本和一个按钮以删除文本。所有这些工作正常,我可以从数据库中删除记录,但我一直无法刷新列表。从光标适配器的ListFragment内更新列表视图
列表看起来有点像这样:
------------------------------------
text text text text | Delete Button|
------------------------------------
text text text text | Delete Button|
------------------------------------
我的第一反应是用这样的:
QCListFragment frag = (QCListFragment) getSupportFragmentManager().findFragmentById(R.id.listFragment);
在什么时候我可以打电话给我的名单片段里面的方法重新 - 查询列表... 但我不能这样做,因为我的适配器不扩展片段。所以我发现很难让片段知道数据已经改变。
无论如何,我最后的想法只是重建活动,但我认为这可能不是最好的事情,并且必须有更好的方法......我确信这是简单的,我我错过了最后一个问题!
(我可以发布更多的代码,如果这将是有益的) 更新: 这里我的整个适配器类
公共类CalcCursorAdapter扩展SimpleCursorAdapter实现的可筛选{
private Context mContext;
private ListView mListView;
private int mLayout;
private Cursor mcursor;
protected static class ViewHolder {
protected TextView text;
protected ImageButton button;
private int position;
}
@SuppressWarnings("deprecation")
public CalcCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mcursor = c;
//mListView = .getListView();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView summary = (TextView)view.findViewById(R.id.calctvPrice);
TextView savings = (TextView)view.findViewById(R.id.calctvSavings);
summary.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcFinalPrice")));
savings.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcSavings")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.calc_list_item, parent, false);
holder.button = (ImageButton) v.findViewById(R.id.qcbtnDelete);
holder.button.setOnClickListener(deleteButton);
holder.position = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
bindView(v, context, cursor);
v.setTag(holder);
return v;
}
private OnClickListener deleteButton = new OnClickListener() {
public void onClick(View v){
View view = (View) v.getParent();
ViewHolder holder = (ViewHolder) view.getTag();
int position = holder.position;
DbHelper mDbHelper;
mDbHelper = new DbHelper(mContext);
mDbHelper.open();
mDbHelper.deleteCalc(position);
mDbHelper.close();
String test = Integer.toString(position);
Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
};
public long qcItemId(int position) {
return position;
}
}
任何非常感谢帮助!
谢谢。
你试过notifyDatasetChanged()方法吗? – Varundroid
该片段不处理点击,适配器,这是因为被点击的按钮是在列表中。并意味着(至少据我所知)我不能调用adapter.notifyDatasetChanged(),因为我在适配器内... – Klaven
看看我的答案,让我知道如果你现在可以打电话notifyDatasetChanged()方法。我们会尽力帮助你。 – Varundroid