【问题标题】:Refresh listview after SQLite update - Requery with spinnerSQLite 更新后刷新列表视图 - 使用微调器重新查询
【发布时间】:2014-04-15 21:10:58
【问题描述】:

我有一个自定义列表视图,可以显示从 SQLite 数据库中提取的内容。我还可以编辑和更新任何给定行的单个值。我已成功实施更新。

但是,在更新行时,列表视图不会显示更改。我是否应该更改活动并返回更改已实施。据我了解,我需要重新查询光标以同步数据更改。

我的问题是我的列表视图内容可以根据微调器的值(全部、Catatergory1、Catatergory2 等)更改,因此取决于微调器值,我需要一个不同的游标重新查询,然后是适配器上的 notifyDataSetChanged()。

例如

假设我有一个食物数据库:

我可以在显示所有行时编辑和更新行值(未选择类别)。 但是也 I can edit and update a row value when category selected is fruit.


因此,我必须根据微调器类别值运行特定的游标重新查询。

我试图调用我的原始类,通过一系列“if 语句”来确定微调器值,然后选择要运行的正确游标,但这要求方法是静态的,这在返回微调器时是不允许的价值。

请指教。

【问题讨论】:

    标签: android sqlite listview android-listview


    【解决方案1】:

    我有一个可以帮助你的代码,我用它来刷新一个城市列表视图,具体取决于上一个微调器上选择的状态

    final stateAdapter stateadapter = new stateAdapter(this, StateList);
    //customized adapter of state, it´s just an integer and a String
    
        StateSpinner = (Spinner) findViewById(R.id.spinner1);
        StateSpinner.setAdapter(stateadapter);
    
        StateSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                State state = (State) StateSpinner.getSelectedItem();
                                //Custom State Class, you may use your custom food class
                ArrayList<City> CitiesList = new ArrayList<City>();
    
                try {
                                //your new SQLite query to populate the CitiesList
    
                    }
                } catch (Exception ex) {
    
                }
    
                cityadapter = new cityAdapter(MyActivity.this, CitiesList);
                                //customized adapter for cities
                CitySpinner = (Spinner) findViewById(R.id.spinner2);
                CitySpinner.setAdapter(cityadapter);
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }
        });
    

    【讨论】:

    • 我认为这不适用于我的问题。我可以根据选择的类别更改我的列表视图读数。在给定类别的数据库更改后,我不知道如何刷新列表视图。
    【解决方案2】:

    我找到了重新查询的替代方法,并在使用给定值更改数据库后手动更新了数组/适配器,然后运行 ​​notifyDataSetChanged() 命令刷新列表视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多