ActionBar中的微调器没有更新ListView

时间:2013-11-23 11:26:44

标签: android android-listview android-actionbar android-spinner android-holo-everywhere

我有两个要素:

  1. 我的活动设置的动作栏(HoloEverywhere(support.v7))中的旋转器onItemSelectedListener()
  2. 我的Activity中的Listview,其中填充了ArrayAdapter
  3. 当我从Spinner中选择一个Item时,应该更新ListView。但不知怎的,我想我的Spinner有问题。在选择SpinnerItem后,当我在Activity的TextField中单击时,ListView首先更新。

    我的代码:

    // Get a databaseConnection and fills the history variable with the data of 
    // the selected spinnerItem "history" is right filled with all Strings for 
    // the chosen Spinner from the database -> works correct
    
    public void updateHistory() {
        sqlLightDatabase database = new sqlLightDatabase(this);
        ArrayList<String> history = database.getArrayList(
                                        mySpinner.getSelectedItem().toString());
    
    
        // set the adapter to the ListView
        ArrayAdapter<String> listenAdapter = new ArrayAdapter<String>(this,
                                                      R.layout.list_item, history);
        myListView.setAdapter(listenAdapter);
    
        // the code works through, but the ListView is not shown the update 
        // instantly. But now, when I click a TextField in my Activity, myListView 
        // shows the new data
    }
    
    // Is instantly called when an Item is selected in mySpinner (in my ActionBar)
    
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
                               long id) 
    {
        // the updateHistory function is called but myLitstView doesn't get
        // updated with new Data
    
        updateHistory();
    
        //Just for testing. The following is working instantly fine.
        drivenDistance_TF.setText("History changed");
    }
    

    我尝试了最近两天修复此问题,但我完全不知道我还能尝试立即更新ListView。

    没有ErrorLog,因为没有错误。

1 个答案:

答案 0 :(得分:0)

您必须调用notifyDataSetChanged方法来更新列表视图中的数据

所以写下这一行

listenAdapter.notifyDataSetChanged();
myListView.setAdapter(listenAdapter);后的

在updateHistory();