【问题标题】:Android: changing tab must refresh my listview of the dbAndroid:更改选项卡必须刷新我的数据库列表视图
【发布时间】:2012-07-10 09:28:47
【问题描述】:

我有一个使用标签主机制作的应用程序。 单击时我需要刷新我的列表视图(第二个选项卡)。 此视图包含数据库数据,很明显它需要始终保持最新(因为在整个应用程序中我进行更新、插入、删除、... 我该怎么办?

这是选项卡调用的类中的onCreate

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bookmarks); 
    final Database info=new Database(this); 
    final Timer t = new Timer(); 
    info.open(); 
    final Cursor data=info.getData(); 
    final ListView listView1 = (ListView) findViewById(R.id.list_mia); 
    String[] from = new String[] {"contenuto"};
    int[] to = new int[] { android.R.id.text1 };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, data, from, to); 
    listView1.setAdapter(adapter);

这是选项卡类(“涉案类是书签”)

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);
    TabHost tabHost = getTabHost();
    // Tab per lettore
    TabSpec lettoretab = tabHost.newTabSpec("Lettore");
    lettoretab.setIndicator("Lettore", getResources().getDrawable(R.drawable.lettore));
    Intent lettoreIntent = new Intent(this, Lettore.class);
    lettoretab.setContent(lettoreIntent);
    // Tab per bookmarks
    TabSpec bookmarkstab = tabHost.newTabSpec("Bookmarks");
    bookmarkstab.setIndicator("Bookmarks", getResources().getDrawable(R.drawable.bookmarks));
    Intent bookmarksIntent = new Intent(this, Bookmarks.class);
    bookmarkstab.setContent(bookmarksIntent);
    // Tab per create
    TabSpec createtabs = tabHost.newTabSpec("Create");
    createtabs.setIndicator("Create", getResources().getDrawable(R.drawable.create));
    Intent createIntent = new Intent(this, Create.class);
    createtabs.setContent(createIntent);
    // Tab per feed
    TabSpec feedtabs = tabHost.newTabSpec("Feed");
    feedtabs.setIndicator("Feed", getResources().getDrawable(R.drawable.feed));
    Intent feedIntent = new Intent(this, Feeds.class);
    feedtabs.setContent(feedIntent);
    // Tab per actionmobileapp
    TabSpec actionmobileapptabs = tabHost.newTabSpec("App");
    actionmobileapptabs.setIndicator("App", getResources().getDrawable(R.drawable.actionmobileapp));
    Intent actionmobileappIntent = new Intent(this, Actionmobileapp.class);
    actionmobileapptabs.setContent(actionmobileappIntent);
    // Aggiungo i tabs
    tabHost.addTab(lettoretab);
    tabHost.addTab(bookmarkstab);
    tabHost.addTab(createtabs);
    tabHost.addTab(feedtabs);
    tabHost.addTab(actionmobileapptabs);

我已经尝试调用 onResume 中的代码,但没有成功...

【问题讨论】:

    标签: android database tabs refresh


    【解决方案1】:
    TabHost tabHost = getTabHost();
                Intent intent = new Intent(this, c);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
                View tabIndicator = LayoutInflater.from(this).inflate(
                        R.layout.tab_indicator, getTabWidget(), false);
                TextView title = (TextView) tabIndicator.findViewById(R.id.title);
                title.setText(labelId);
                ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
                icon.setImageResource(drawableId);
    
                spec.setIndicator(tabIndicator);
                spec.setContent(intent);
                tabHost.addTab(spec);
    

    将此添加到 addTab() 内的 TabActivity 类中。

    谢谢....

    【讨论】:

    • 对不起,我不明白你的意思:我已经用标签类编辑了我的问题
    • 请在 TabActivity 类的 addTab() 中添加 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)。
    【解决方案2】:

    这可能与此重复:

    SimpleCursorAdapter not Updating with DB Changes

    您应该查看您想要使用的 LoaderManager 的 Android 文档。

    LoaderManager Class Reference with Code Example

    【讨论】:

    • 不是真的。重新查询或其他事情不是我的问题。我无法以这种方式调用光标或重新查询。此外,视图不会改变。
    【解决方案3】:

    在 'Activity' 和 onResume 中将 'adpater' 变量设为 public 调用这个 mettadapter.swapCursor(c) 这里 c 是您新更新的光标。之后别忘了拨打notifyDataSetChanged()

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2015-05-17
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多