我有两个要素:
onItemSelectedListener()
。当我从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,因为没有错误。
答案 0 :(得分:0)
您必须调用notifyDataSetChanged方法来更新列表视图中的数据
所以写下这一行
listenAdapter.notifyDataSetChanged();
myListView.setAdapter(listenAdapter);
后的在updateHistory();
中