notifyDataSetChanged()不更新我的阵列我的微调我的、阵列、notifyDataSetChanged

2023-09-04 05:45:54 作者:心仅存一人/。

问:如果我之前,设置一个变量的onCreate 我可以使用 notifyDataSetChanged()更新适配器使用该阵列以后呢?

我鼓动我的 city_values​​阵列之前,我的的onCreate - 这是我能拿到剧本的唯一途径不显示任何错误。但是,一旦用户从它的微调选择一个状态,它应该使用 notifyDataSetChanged()更新,重视的city_values​​阵列适配器。下面是我的code一小部分。我想我的问题已经做了city_value被设置为早。我怎样才能解决这个?

 公共类SearchActivity延伸活动{
    ArrayAdapter<字符串>适配器2;
    字符串city_values​​ [] =新的String [] {请首先选择的状态。};

      @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.search_layout)

           适配器2 =新的ArrayAdapter<字符串> (这一点,android.R.layout.simple_spinner_item,city_values​​);
           adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
           city​​spinner.setAdapter(适配器2);


//在选择国家微调使用项目价值进行查询,并得到该市重新分配这些值回city_values​​然后告诉适配器2 notifyDataSetChanged()


           的for(int i = 0; I< jsonArray.length();我++)
            {
                串styleValue = jsonArray.getJSONArray(ⅰ).getString(0);
                Log.d(TAG,styleValue);
                city​​_spinner [我] = styleValue;
            }
               city​​_values​​ = city_spinner;

               adapter2.notifyDataSetChanged();
 

解决方案

这将是更好声明一个ArrayList,然后添加在ArrayList中的内容和数据集的适配器,并通知。

 公共类SearchActivity延伸活动{
    ArrayAdapter<字符串>适配器2;
    ArrayList的<字符串> city​​_values​​ =新的ArrayList<字符串>();

      @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.search_layout)

            city​​_values​​.add(内容);

           适配器2 =新的ArrayAdapter<字符串> (这一点,android.R.layout.simple_spinner_item,city_values​​);
           adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
           city​​spinner.setAdapter(适配器2);
 

现在,如果你想更新这个cityspinner所选项目的另一个微调,您可以以同样的方式又ArrayList和该添加的项目,并设置适配器。

 更新
 
电脑开机后找不到阵列里的硬盘了

取一个的ArrayList<字符串> city​​_spinner_array =新的ArrayList<字符串取代;

 的for(int i = 0; I< jsonArray.length();我++)
            {
                串styleValue = jsonArray.getJSONArray(ⅰ).getString(0);
                Log.d(TAG,styleValue);
                city​​_spinner_array.add(styleValue);
            }
 

和,现在你将有 city_spinner_array 新值。因此,设置适配器和你做了previous微调。

希望,这会有所帮助, 谢谢...

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();

解决方案

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.

Hope, this helps, Thanks...