Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

java - notifyDataSetChanged() not updating my array for my spinner

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

I am instigating my city_values array prior to my onCreate -This is the only way I can get the script not to show any errors. But once the user selects a state from its spinner it should use notifyDataSetChanged() to update the adapter that attaches the city_values array. Below is a small section of my code. I think my issue has to do with the city_value being set to 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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It would be better to declare an ArrayList and then add the content in 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 spinner on this cityspinner selected item, you can take another ArrayList in the same way and add the items in that and set the Adapter.

UPDATE

Take an 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 your new values in city_spinner_array. So, set the adapter and you did for the previous spinner.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...