实时动态填充 SQLite ListViews,在活动之间,使用 onListItemClicks/intents/other?

Posted

技术标签:

【中文标题】实时动态填充 SQLite ListViews,在活动之间,使用 onListItemClicks/intents/other?【英文标题】:Populating SQLite ListViews Dynamically in Real-time, Between Activities, Using onListItemClicks/intents/other? 【发布时间】:2013-08-14 10:49:04 【问题描述】:

我是 android 的新手,我真的很想了解我的应用程序的基本用户流,这样我可以获得一种成就感,这会让我对它的所有领域都保持足够的兴趣。我知道这可能不是最佳实践,但我稍后会返回并确保我拥有多线程、单例 DB、异步和所有其他好东西。主要是我想尽快进入 UI 设计部分。

基本上,我正在尝试根据 onListItemClicks 更新 SQL 查询,并通过用户导航、作者、关键字、类别等动态更新。

我有这个工作,并确信有一种方法可以使某些东西工作,因为它很容易与数据库相对应,但我还没有找到任何东西:

@Override

protected void onListItemClick(ListView l, View v, int position, long id) Log.i("FragmentList", "authors_id" + id);

这是我尝试过的其他一些代码,但失败了:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 

String selectedItem = (String) getListAdapter().getItem(position);
String query = "SELECT body FROM \"main\".\"quotes\" WHERE _id = '" + selectedItem + "'";

我也尝试了大量不同的意图/捆绑方法

    https://developer.android.com/reference/android/content/Intent.html https://developer.android.com/reference/android/os/Bundle.html

我能够获得几个意图来跨活动传递值,但我不断得到空值和不一致的结果。

【问题讨论】:

【参考方案1】:
    Intent intent = new Intent(MainActivity.this, authorlistactivity.class);
    Bundle bundle = new Bundle();
    bundle.putInt("position", position);
    bundle.putLong("id", id);
    intent.putExtras(bundle);
    startActivity(intent);

将此部分添加到您的下一个活动的创建方法中:

    Bundle bundle = getIntent().getExtras();
    assert bundle != null;
    int position = bundle.getInt("position");
    long id = bundle.getLong("id");

显然查询比我想象的要宽松得多,正如 vogella 所证明的那样:

        // Database creation SQL statement
        private static final String DATABASE_CREATE = "create table "
                + TABLE_TODO
                + "("
                + COLUMN_ID + " integer primary key autoincrement, "
                + COLUMN_CATEGORY + " text not null, "
                + COLUMN_SUMMARY + " text not null,"
                + COLUMN_DESCRIPTION
                + " text not null"
                + ");";

http://www.vogella.com/articles/AndroidSQLite/article.html

【讨论】:

以上是关于实时动态填充 SQLite ListViews,在活动之间,使用 onListItemClicks/intents/other?的主要内容,如果未能解决你的问题,请参考以下文章

Android:SQLite 和 ListViews

如何在 Android Studio 中创建动态 ListView

Android ViewPager + 带有动态 ListViews 的片段

包含静态预填充和动态用户数据的核心数据

Python sqlite3 构建带有动态占位符的部分

用 javascript 填充输入搜索字段(字段动态显示结果)