notifyDataSetChanged() 没有更新我的微调器的数组

发布于 2024-12-05 02:32:34 字数 1427 浏览 0 评论 0原文

问题:如果我在 onCreate 之前设置变量,我可以使用 notifyDataSetChanged() 来更新稍后使用该数组的适配器吗?

我在 onCreate 之前煽动我的 city_values 数组 - 这是让脚本不显示任何错误的唯一方法。但是,一旦用户从其微调器中选择了一个状态,就应该使用 notifyDataSetChanged() 来更新附加 city_values 数组的适配器。下面是我的代码的一小部分。我认为我的问题与 city_value 设置得太早有关。我该如何解决这个问题?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2024-12-12 02:32:34

最好先声明一个ArrayList,然后添加ArrayList中的内容,并将数据设置到适配器并通知。

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

现在,如果您想更新此 cityspinner 所选项目上的另一个微调器,您可以以相同的方式获取另一个 ArrayList 并在其中添加项目并设置适配器。

UPDATE

取一个ArrayList; city_spinner_array = new ArrayList;

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

现在,您将在 city_spinner_array 中获得新值。因此,设置适配器就像之前的微调器一样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文