2013-05-21 78 views
1

在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; 
} 

}

任何非常感谢帮助!

谢谢。

+0

你试过notifyDatasetChanged()方法吗? – Varundroid

+0

该片段不处理点击,适配器,这是因为被点击的按钮是在列表中。并意味着(至少据我所知)我不能调用adapter.notifyDatasetChanged(),因为我在适配器内... – Klaven

+0

看看我的答案,让我知道如果你现在可以打电话notifyDatasetChanged()方法。我们会尽力帮助你。 – Varundroid

回答

0

我想你误解了Adapter和Fragment的概念。您可以在Fragment中将Adapter设置为Fragment或AdapterView。查询数据后,调用Adapter.notifyDataSetChanged(),然后刷新Fragment或AdapterView。

+0

我的适配器内没有适配器对象...所以我不知道如何执行所述操作。我的onclick监听器位于适配器内部,因为它是每行中的一个按钮。 – Klaven

+0

你不需要适配器内的Adapter对象,只需调用notifyDataSetChanged()。 – Varundroid

+0

我在OnClickListener内部的适配器中调用了notifyDataSetChanged(),它不会执行任何操作。我上面发布了我的代码。 – Klaven

0

当您位于适配器内时,您可以直接拨打notifyDatasetChanged()。您不需要适配器的对象。我认为你需要刷新你的OOPS概念。你的适配器扩展了BaseAdapter或ArrayAdapter或SimpleAdapter,它们都有这个方法,你不需要类本身的对象来调用它自己的方法。

编辑

notifyDataSetChanged()因为你是膨胀的观点,每次,这是不是在性能方面的好事没为你工作。看看这Answer。在notifyDataSetChanged()之后onContentChanged()被调用。我不确定这是如何为你工作的。我强烈建议你使用notifyDataSetChanged()来使事情发挥作用,不要为了解决问题而努力。