问题描述:

My List view is not updating and I dont know why. It was working and I dont remember changing anything about it.

I have a spinner with two options. Should update differently depending on which option is selected. Neither is working.

The position option is the easiest. It just puts 4 strings in an arraylist and then sets the adapter and calls notifyDataSetChanged. Stepping through it gets into the if loop in onItemSelected, but the data just is not showing in my listview.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_transfer);

searchSelection = (Spinner) findViewById(R.id.searchType);

ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.player_search_type,

android.R.layout.simple_spinner_item);

spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

searchSelection.setAdapter(spinnerAdapter);

searchSelection.setOnItemSelectedListener(this);

resultListView = (ListView) findViewById(R.id.resultList);

results = new ArrayList<>();

listAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, results);

resultListView.setAdapter(listAdapter);

resultListView.setOnItemClickListener(this);

}

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

Log.i("Type", parent.getItemAtPosition(position).toString());

if(parent.getItemAtPosition(position).toString().compareTo("Position") == 0)

{

results.clear();

results.add("Goalkeeper");

results.add("Defender");

results.add("Midfielder");

results.add("Forward");

listAdapter.clear();

listAdapter.addAll(results);

listAdapter.notifyDataSetChanged();

}

网友答案:

You are adding the elements in the list with this code:

results.add("Goalkeeper");
results.add("Defender");
results.add("Midfielder");
results.add("Forward");

And after are removing with this line:

listAdapter.clear();

The result array is already inside the adapter in this point.

相关阅读:
Top