在android中,我有一个自定义适配器,在这个自定义适配器中,我创建了一个带有一些文本的视图和一个删除文本的按钮。所有这些工作正常,我可以从数据库中删除记录,但我无法刷新列表。
该列表看起来有点像这样:
------------------------------------
text text text text | Delete Button|
------------------------------------
text text text text | Delete Button|
------------------------------------
我的第一直觉是使用这样的东西:
QCListFragment frag = (QCListFragment) getSupportFragmentManager().findFragmentById(R.id.listFragment);
在什么时候我可以调用我的列表片段中的方法来重新查询列表......但我不能这样做,因为我的适配器没有扩展片段。所以我发现很难让片段知道数据已更改。
无论如何,我的最后一个想法就是重建活动,但我认为这可能不是最好的事情,必须有更好的方法......我敢肯定这是我想念的简单事情我的最后一个问题是!
(如果有帮助,我可以发布更多代码)更新:这是我的整个适配器类
公共类 CalcCursorAdapter 扩展 SimpleCursorAdapter 实现 Filterable{
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;
}
}
任何帮助深表感谢!
谢谢。