【问题标题】:Add Spinner Hint that has data from json添加具有来自 json 数据的 Spinner Hint
【发布时间】:2017-05-10 20:06:10
【问题描述】:

我从 json 数据填充微调器,我想设置提示,以便用户可以理解必须选择的内容,因为当没有互联网连接时微调器显示没有选择的迹象。一些答案建议使用提示

 <Spinner
    android:layout_width="match_parent"
    android:id="@+id/spinner"
    android:prompt="@string/country_prompt"
    />

和其他人建议使用微调器的最后一个索引作为提示,如here。我如何设置提示或相关的内容,以便用户即使在应用程序离线时也能理解微调器的全部含义.下面是我从here 截取的代码

    @Override
    protected Void doInBackground(Void... params) {
        // Locate the WorldPopulation Class 
        world = new ArrayList<WorldPopulation>();
        // Create an array to populate the spinner 
        worldlist = new ArrayList<String>();
        // JSON file URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");

        try {
            // Locate the NodeList name
            jsonarray = jsonobject.getJSONArray("worldpopulation");
            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobject = jsonarray.getJSONObject(i);

                WorldPopulation worldpop = new WorldPopulation();

                worldpop.setRank(jsonobject.optString("rank"));
                worldpop.setCountry(jsonobject.optString("country"));
                worldpop.setPopulation(jsonobject.optString("population"));
                worldpop.setFlag(jsonobject.optString("flag"));
                world.add(worldpop);

                // Populate spinner with country names
                worldlist.add(jsonobject.optString("country"));

            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the spinner in activity_main.xml
        Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);

        // Spinner adapter
        mySpinner
                .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_dropdown_item,
                        worldlist));

        // Spinner on item click listener
        mySpinner
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0,
                            View arg1, int position, long arg3) {
                        // TODO Auto-generated method stub
                        // Locate the textviews in activity_main.xml
                        TextView txtrank = (TextView) findViewById(R.id.rank);
                        TextView txtcountry = (TextView) findViewById(R.id.country);
                        TextView txtpopulation = (TextView) findViewById(R.id.population);

                        // Set the text followed by the position 
                        txtrank.setText("Rank : "
                                + world.get(position).getRank());
                        txtcountry.setText("Country : "
                                + world.get(position).getCountry());
                        txtpopulation.setText("Population : "
                                + world.get(position).getPopulation());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
                    }
                });
    }
}

【问题讨论】:

    标签: android json spinner android-spinner


    【解决方案1】:

    首先在 ArrayList 0 索引中添加你的后,它将被显示:

        worldlist = new ArrayList<String>();
        worldlist.add("Your first hint");
    

    对于最后一个提示,在 for 循环之后添加您的提示:

    try{
        for(......){
    
      }
      worldlist.add("Your last hint");
     }catch(Exception e){ }
    

    【讨论】:

      【解决方案2】:
      @Override
      protected void onPostExecute(Void args) {
          // Locate the spinner in activity_main.xml
          Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
      
      //add a check if worldlist is empty. Initialize it where you have declared it.
      if(!worldlist.size() > 0){
          worldlist.add(“select country”);
      }
      
      // Spinner adapter
      mySpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,
              android.R.layout.simple_spinner_dropdown_item, worldlist));
      
      //your rest of the code here. . . 
      

      }

      【讨论】:

        【解决方案3】:

        试试这个

            @Override
                protected Void doInBackground(Void... params) {
                    // Locate the WorldPopulation Class 
                    world = new ArrayList<WorldPopulation>();
                    // Create an array to populate the spinner 
                    worldlist = new ArrayList<String>();
                    worldlist.add("Selct Country");
                    // JSON file URL address
                    jsonobject = JSONfunctions
                            .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
        
                    try {
                        // Locate the NodeList name
                        jsonarray = jsonobject.getJSONArray("worldpopulation");
                        for (int i = 0; i < jsonarray.length(); i++) {
                            jsonobject = jsonarray.getJSONObject(i);
        
                            WorldPopulation worldpop = new WorldPopulation();
        
                            worldpop.setRank(jsonobject.optString("rank"));
                            worldpop.setCountry(jsonobject.optString("country"));
                            worldpop.setPopulation(jsonobject.optString("population"));
                            worldpop.setFlag(jsonobject.optString("flag"));
                            world.add(worldpop);
        
                            // Populate spinner with country names
                            worldlist.add(jsonobject.optString("country"));
        
                        }
                    } catch (Exception e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }
                    return null;
                }
        
        @Override
            protected void onPostExecute(Void args) {
                // Locate the spinner in activity_main.xml
                Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
        
                // Spinner adapter
                mySpinner
                        .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                                android.R.layout.simple_spinner_dropdown_item,
                                worldlist));
        
                // Spinner on item click listener
                mySpinner
                        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        
                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int position, long arg3) {
                                // TODO Auto-generated method stub
                                // Locate the textviews in activity_main.xml
                                if(position > 0){
                                TextView txtrank = (TextView) findViewById(R.id.rank);
                                TextView txtcountry = (TextView) findViewById(R.id.country);
                                TextView txtpopulation = (TextView) findViewById(R.id.population);
        
                                // Set the text followed by the position 
                                txtrank.setText("Rank : "
                                        + world.get(position).getRank());
                                txtcountry.setText("Country : "
                                        + world.get(position).getCountry());
                                txtpopulation.setText("Population : "
                                        + world.get(position).getPopulation());
                                    }
                            }
        
                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub
                            }
                        });
            }
        }
        

        【讨论】:

          【解决方案4】:

          使用此代码更新,在数组 0 位置添加提示。

          @Override
              protected Void doInBackground(Void... params) {
                  // Locate the WorldPopulation Class 
                  world = new ArrayList<WorldPopulation>();
                  // Create an array to populate the spinner 
                  worldlist = new ArrayList<String>();
                  worldlist.add(0,"your hint");
                  // JSON file URL address
                  jsonobject = JSONfunctions
                          .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
          
                  try {
                      // Locate the NodeList name
                      jsonarray = jsonobject.getJSONArray("worldpopulation");
                      for (int i = 0; i < jsonarray.length(); i++) {
                          jsonobject = jsonarray.getJSONObject(i);
          
                          WorldPopulation worldpop = new WorldPopulation();
          
                          worldpop.setRank(jsonobject.optString("rank"));
                          worldpop.setCountry(jsonobject.optString("country"));
                          worldpop.setPopulation(jsonobject.optString("population"));
                          worldpop.setFlag(jsonobject.optString("flag"));
                          world.add(worldpop);
          
                          // Populate spinner with country names
                          worldlist.add(jsonobject.optString("country"));
          
                      }
                  } catch (Exception e) {
                      Log.e("Error", e.getMessage());
                      e.printStackTrace();
                  }
                  return null;
              }
          
              @Override
              protected void onPostExecute(Void args) {
                  // Locate the spinner in activity_main.xml
                  Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
          
                  // Spinner adapter
                  mySpinner
                          .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                                  android.R.layout.simple_spinner_dropdown_item,
                                  worldlist));
          
                  // Spinner on item click listener
                  mySpinner
                          .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          
                              @Override
                              public void onItemSelected(AdapterView<?> arg0,
                                      View arg1, int position, long arg3) {
                                  // TODO Auto-generated method stub
                                  // Locate the textviews in activity_main.xml
                                  TextView txtrank = (TextView) findViewById(R.id.rank);
                                  TextView txtcountry = (TextView) findViewById(R.id.country);
                                  TextView txtpopulation = (TextView) findViewById(R.id.population);
          
                                  // Set the text followed by the position 
                                  txtrank.setText("Rank : "
                                          + world.get(position).getRank());
                                  txtcountry.setText("Country : "
                                          + world.get(position).getCountry());
                                  txtpopulation.setText("Population : "
                                          + world.get(position).getPopulation());
                              }
          
                              @Override
                              public void onNothingSelected(AdapterView<?> arg0) {
                                  // TODO Auto-generated method stub
                              }
                          });
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多