NotifyDataSetChanged () does not update my array for my counter

QUESTION: If I set the variable to onCreate , can I use notifyDataSetChanged() to update the adapter that uses this array later?

I am inciting my city_values array to my onCreate . This is the only way to make the script not show any errors. But as soon as the user selects the state from his counter, he should use notifyDataSetChanged() to update the adapter, which attaches the city_values ​​array. Below is a small section of my code. I think my problem is that city_value is set early. How can I get around this?

 public class SearchActivity extends Activity{ ArrayAdapter<String> adapter2; String city_values[] = new String[]{"Please select a state first."}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search_layout) adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values); adapter2.setDropDownViewResource(R.layout.city_spinner_layout); cityspinner.setAdapter(adapter2); //On select of State spinner use item value to query and get citys reassign those values back to city_values and then tell adapter2 notifyDataSetChanged() for (int i=0; i<jsonArray.length(); i++) { String styleValue = jsonArray.getJSONArray(i).getString(0); Log.d(TAG, styleValue); city_spinner[i] = styleValue; } city_values = city_spinner; adapter2.notifyDataSetChanged(); 
0
source share
1 answer

It is better to declare an ArrayList and then add the content to the ArrayList and set the data to the adapter and notify.

  public class SearchActivity extends Activity{ ArrayAdapter<String> adapter2; ArrayList<String> city_values = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.search_layout) city_values.add("your content"); adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values); adapter2.setDropDownViewResource(R.layout.city_spinner_layout); cityspinner.setAdapter(adapter2); 

Now, if you want to update another counter on this selected cityspinner element, you can take another ArrayList in the same way and add elements to it and install the Adapter.

 UPDATE 

Take ArrayList<String> city_spinner_array = new ArrayList<String>;

 for (int i=0; i<jsonArray.length(); i++) { String styleValue = jsonArray.getJSONArray(i).getString(0); Log.d(TAG, styleValue); city_spinner_array.add(styleValue); } 

And now you will have new values ​​in city_spinner_array . So, install the adapter, and you did it for the previous counter.

Hope this helps, Thanks ...

+1
source

All Articles