【问题标题】:Inserting to a database from listview with OnOptionItemSelected使用 OnOptionsItemSelected 从列表视图插入数据库
【发布时间】:2014-12-01 15:44:42
【问题描述】:

我需要一些帮助。 android编程还是有点新意,不知道能不能做到。

我正在做的事情的要点:我目前正在从 RESTful API 中获取数据,该 API 将数据作为 json 数组返回。我获取数据并将其泵入表格,然后使用适配器并在列表视图中显示所有数据。

数据如下所示(将 x 替换为数据):

[{
id: xxx,
number: "xxxxx",
title: "xxxxxxx",
definition: "xxxxxxx",
status: "xxxxx",
name: "xxxxx"
},

问题来了:从listview中,我想取数据,而onItemSelect,我想把选中的数据插入到一个单独的表中。用户可以选择一个或多个listview,然后从onoptionitemselect中单击一个menuItem,它将数据插入到表中。

我有两个主要问题: 1.我目前在setOnItemClickListener中使用这个代码:

String item = adapterView.getItemAtPosition(position).toString();

它以数组的形式抽出。我可以从数组中解析每个单独的元素吗? (根据数据,我想提取 id、number、title 等数据) 2.我不确定这是否有效,但是在我选择了项目之后,我可以使用 onOptionsItemSelected 菜单项来告诉应用程序将数据插入到新表中吗?

这就是我现在所拥有的:

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.menu_favorite){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

感谢您的所有帮助。这是一次很棒的学习经历,我希望能学到更多。

谢谢!

【问题讨论】:

    标签: android listview android-sqlite


    【解决方案1】:

    我的处理方式是维护一个被选中的 id 的 arrayList。

    在onItemClick 中,将位置移动到这个arrayList。

    然后在 onOptionsItemSelected 中,获取 arrayList 内容并使用它来将数据推送到 DB

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多