本文介绍了在CursorAdapter的改变光标无法正常更新列表项的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题很相似,在计算器上很多其他的问题,但我认为它值得自己的职位,由于相比,这里的其他问题的方法是不同的。

This question is very similar to many other questions on StackOverflow, but I think it merits its own post due to a difference in approach compared to other questions here.

我有扩展的CursorAdapter 自定义适配器。我更新的数据,并以刷新光标呼吁有NotifyChange 上相应的URI。在调试时,我可以看到,这是正常工作。

I have a custom adapter that extends CursorAdapter. I'm updating the data and calling notifyChange on the appropriate URI in order to refresh the cursor. In debugging, I can see that this is working properly.

不幸的是,我的列表视图不被创建。我需要他们重新创建的原因是因为我在适配器 NewView的实施

Unfortunately, my list views are not being recreated. The reason that I need them to be recreated is because of my newView implementation on the adapter:

public View newView(final Context context, final Ingredient ingredient, final ViewGroup parent) {
    final int layout = ingredient.isOwned() ? LAYOUT_OWNED : LAYOUT_UNOWNED;
    final View view = LayoutInflater.from(context).inflate(layout, null);

    // ... ViewHolder magic

    return view;
}

问题是,调用有NotifyChange 正在更新列表数据,但不是重新创建,我通过 NewView的

The problem is that calling notifyChange is updating the list data, but it is not recreating the view that I need via newView.

下面是一些其他的事情我试过,同样没有工作:

Here are some other things I tried that equally did not work:


  • 名为 adapter.notifyDataSetChanged()中的数据被更新了。

  • 名为 contentResolver.notifyChange(...)然后按 adapter.notifyDataSetChanged()中的数据被更新后

  • 名为 adapter.changeCursor(newCursor)然后按 adapter.notifyDataSetChanged()

  • 名为 view.invalidate()上改变了特定的列表项。

  • called adapter.notifyDataSetChanged() after the data was updated.
  • called contentResolver.notifyChange(...) followed by adapter.notifyDataSetChanged() after the data was updated.
  • called adapter.changeCursor(newCursor) followed by adapter.notifyDataSetChanged().
  • called view.invalidate() on the specific list item that changed.

这是如何处理这个任何其他建议?

Any other suggestions on how to approach this?

修改:我可能总共才服用错误的做法。我误解的适配器内欣赏到回收利用,而且我看到,回收时正在使用的错误类型的视图。因此,我可能需要去了解如何使用另一种方法造型我的看法。我是以这种方式摆在首位的原因是由于我的愿望,使用样式,不能被编程设置以外充气视图作为的解释。我愿意将采取这个答案的优势,同时适当地回收列表项意见的做法。

Edit: I may altogether just be taking the wrong approach. I misunderstood the recycling of views within adapters, and I see that the wrong type of view is being used when recycled. As such, I'll probably need to go about styling my views using another approach. The reason I was taking this approach in the first place is due to my desire to use styles, which can't be set programmatically other than inflating a view as explained in this StackOverflow question. I'm open to approaches that will take advantage of that answer while properly recycling list item views.

推荐答案

我结束了基于。我需要重写 getItemTypeCount getItemViewType 我的适配器。

I ended up figuring out the problem based upon this StackOverflow answer. I needed to override getItemTypeCount and getItemViewType on my adapter.

@Override
public int getItemViewType(int position) {
    return getItem(position).isOwned() ? 1 : 0;
}

@Override
public int getViewTypeCount() {
    return 2;
}

这篇关于在CursorAdapter的改变光标无法正常更新列表项的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:06