【问题标题】:Update listview inside ListFragment from Cursor Adapter从光标适配器更新 ListFragment 内的列表视图
【发布时间】:2013-05-22 08:42:38
【问题描述】:

在 android 中,我有一个自定义适配器,在这个自定义适配器中,我创建了一个带有一些文本的视图和一个用于删除文本的按钮。所有这些工作正常,我可以从数据库中删除记录,但我无法刷新列表。

列表看起来有点像这样:

------------------------------------
text text text text | Delete Button|
------------------------------------
text text text text | Delete Button|
------------------------------------

我的第一直觉是使用这样的东西:

QCListFragment frag = (QCListFragment) getSupportFragmentManager().findFragmentById(R.id.listFragment);

什么时候我可以调用列表片段中的方法来重新查询列表... 但我不能这样做,因为我的适配器没有扩展 Fragment。所以我发现很难让片段知道数据已被更改。

无论如何,我的最后一个想法是重建活动,但我认为这可能不是最好的做法,必须有更好的方法......我相信这很简单,我'我错过了,就像我上一个问题一样!

(如果有帮助,我可以发布更多代码) 更新: 这是我的整个适配器类

公共类 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;
}

}

非常感谢任何帮助!

谢谢。

【问题讨论】:

  • 你试过 notifyDatasetChanged() 方法了吗?
  • 片段不处理点击,适配器处理,这是因为被点击的按钮在列表内。并且意味着(至少据我所知)我不能调用 adapter.notifyDatasetChanged() 因为我在适配器内部......
  • 看看我的答案,如果您现在可以调用 notifyDatasetChanged() 方法,请告诉我。我们会尽力为您提供帮助。
  • 谢谢。我试过你说的,只是添加调用......它没有抛出任何错误,但它也没有更新列表。它什么也不做。
  • 这可能是因为您的数据集尚未更改。请发布您的适配器代码和删除该项目的代码,以便我看看有什么问题。干杯!!

标签: android android-listview


【解决方案1】:

我认为您误解了适配器和片段的概念。您可以在 Fragment 中将 Adapter 设置为 Fragment 或 AdapterView。重新查询数据后,调用 Adapter.notifyDataSetChanged(),Fragment 或 AdapterView 将被刷新。

【讨论】:

  • 我的适配器内部没有适配器对象...所以我不确定我将如何执行所述操作。我的 onclick 监听器在我的适配器内部,因为它是每一行中的一个按钮。
  • 适配器内部不需要Adapter对象,调用notifyDataSetChanged()即可。
  • 我在 OnClickListener 内的适配器内调用 notifyDataSetChanged() ,但它没有执行任何操作。我已经在上面发布了我的代码。
【解决方案2】:

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

编辑

notifyDataSetChanged() 对您不起作用,因为您每次都在膨胀视图,这在性能方面不是一件好事。看看这个AnsweronContentChanged()notifyDataSetChanged() 之后被调用。我不确定这对你有什么用。我强烈建议你使用notifyDataSetChanged() 来让事情顺利进行,不要到处乱跑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2014-12-05
    相关资源
    最近更新 更多