安卓:微调数据的变化后不更新 [英] Android: Spinner does not update after change of dataset

查看:158
本文介绍了安卓:微调数据的变化后不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的蜘蛛显示活动项目(我用的支持库加载器加载它们)。微调的Init工作正常,这表明仅在活动项目。

但我得到了followeing问题:
当我编辑其他活动的项目,并将它们存储(主动和deactivat其中的一些)的返回(返回按钮),然后我看到飞旋项目的老字号名单。该适配器与得到正确的数据新的光标,但微调不刷新他的名单。

我能做些什么来刷新微调列表?

的onCreate()我初始化微调

  _ItemSpinner =(微调)getView()findViewById(R.id.spn_itemss)。
_ItemAdapter =新AdvancedCursorAdapter(
    getActivity(),
    R.layout.spinner_2line_item,
    空,_ColumnList,_ColumnViews,0);
_ItemSpinner.setAdapter(_ItemAdapter);

onResume()我启动装载器初始化

  @覆盖
公共无效onResume(){
    getLoaderManager()initLoader(1,null,则此)。
    super.onStart();
}

onLoadFinished(装载机<&光标GT;装载机,光标数据)我换光标适配器

  _ItemAdapter.swapCursor(数据);


解决方案

最后我发现,不为什么,但是,元素的 摧毁 的该微调的刷新。

我用同样的ContentProvider两种不同的URI更新所有项目,并仅显示有效的项目


  1. CONTENT_URI_ITEMS

  2. CONTENT_URI_ACTIVE_ITEMS

如果我使用相同的URI更新和显示(与需要有源滤波器),微调列表将被更新。如果我使用不同的URI,微调表演的 的数据。

我不明白这种行为(光标返回项目的正确的号码,但微调不关心),但我有一个变通的作品。

存储数据:

  getActivity()。getContentResolver()。更新(
    RecordsContentProvider.CONTENT_URI_ITEMS,
    _Changes,
    COLUMN_ID +=?,
    新的String [] {将String.valueOf(_ID)});

在(获取数据) - 不起作用

  CursorLoader curLoader =新CursorLoader(this.getActivity()
    RecordsContentProvider.CONTENT_URI_ACTIVE_ITEMS,
    _ITEMS_PROJECTION,
    NULL,NULL,NULL);

在(获取数据) - 工作

  CursorLoader curLoader =新CursorLoader(this.getActivity()
    RecordsContentProvider.CONTENT_URI_ITEMS,
    _ITEMS_PROJECTION,
    COLUMN_IS_ACTIVE += 1,NULL,NULL);

notifyDataSetChanged 是没有必要的,如果标的的ContentProvider 为你做这个是这样的:(的getContext()getContentResolver()有NotifyChange(URI,NULL);

In my app the spinne shows the active items (I use Loader of support library to load them). The init of spinner works fine and it shows only the active items.

But I got the followeing problem: When I edit the items in another activity and store them (active and deactivat some of them) an go back (Back-Button), then I see the "old" list of items in the spinner. The adapter get the new cursor with correct data, but the spinner does not refresh his list.

What can I do to refresh the spinner list?

In onCreate() I initialize the Spinner

_ItemSpinner = (Spinner) getView().findViewById(R.id.spn_itemss);
_ItemAdapter = new AdvancedCursorAdapter(
    getActivity(),
    R.layout.spinner_2line_item,
    null, _ColumnList, _ColumnViews, 0);
_ItemSpinner.setAdapter(_ItemAdapter);

In onResume() I start the loader initialisation

@Override
public void onResume() {
    getLoaderManager().initLoader(1, null, this);
    super.onStart();
}

In onLoadFinished(Loader<Cursor> loader, Cursor data) I swap the cursor for the adapter

_ItemAdapter.swapCursor(data);

解决方案

Finally I found out, not why, but which, element destroy the refresh of the spinner.

I used two different URIs of the same ContentProvider for updating all items and showing only active items

  1. CONTENT_URI_ITEMS
  2. CONTENT_URI_ACTIVE_ITEMS

If I use the same URI for updating and showing (with needed active filter), the spinner list will be updated. If I use different URIs, the spinner shows old data.

I don't understand this behavior (the cursor returns correct number of items, but the spinner doesn't care), but I have a work around that works.

Store data:

getActivity().getContentResolver().update(
    RecordsContentProvider.CONTENT_URI_ITEMS,
    _Changes,
    COLUMN_ID + " =?",
    new String[] { String.valueOf(_ID) });

Before (get data) - doesn't work:

CursorLoader curLoader = new CursorLoader(this.getActivity(),
    RecordsContentProvider.CONTENT_URI_ACTIVE_ITEMS,
    _ITEMS_PROJECTION,
    null, null, null);

After (get data) - work:

CursorLoader curLoader = new CursorLoader(this.getActivity(),
    RecordsContentProvider.CONTENT_URI_ITEMS,
    _ITEMS_PROJECTION,
    COLUMN_IS_ACTIVE + " =1", null, null);

notifyDataSetChanged is not necessary, if the underlying ContentProvider do this for you like this: (getContext().getContentResolver().notifyChange(uri, null);)

这篇关于安卓:微调数据的变化后不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆