如何填充微调器而不是文本视图?不知道从哪里开始

368yc8dk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(232)

我可以将json数组解析为textview,但我想填充一个微调器。目前,我正在使用以下代码将json解析为textview。理想情况下,我想有一个微调器,列出了“名称”的结果,并把“体积”,“全重”和“空重量”到单独的文本视图的基础上选择的项目。这是我第一次尝试在应用程序中加入微调器,但我不知道从哪里开始。

public void getJSON(){
    String url = "https://example.com/DBReadJSON.php";
    JsonRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("bottles");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject bottles = jsonArray.getJSONObject(i);
                            String Name = bottles.getString("name");
                            int vol = bottles.getInt("volume");
                            int full = bottles.getInt("fullweight");
                            int empty = bottles.getInt("emptyweight");
                            mTextViewResult.append(Name + ", " + vol + ", " + full +", " + empty + "\n");
                            Toast.makeText(getApplicationContext(),Name, Toast.LENGTH_SHORT).show();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    mQueue.add(request);
}

我意识到这可能是一个愚蠢的问题,我在解析json时完全错了。很抱歉。我会很感激有人指导我如何做到这一点。
谢谢。

h22fl7wq

h22fl7wq1#

代码取自https://www.tutorialspoint.com/android/android_spinner_control.htm.

class AndroidSpinnerExampleActivity extends Activity implements  OnItemSelectedListener{

   Spinner spinner;
   ArrayAdapter<String> dataAdapter;

   List<String> categories;

   public void update(String add){
      categories.add(add);

      dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(dataAdapter);
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      // Spinner element
      spinner = (Spinner) findViewById(R.id.spinner);

      // Spinner click listener
      spinner.setOnItemSelectedListener(this);

      // Spinner Drop down elements
      categories = new ArrayList<String>();
      categories.add("Automobile");
      categories.add("Business Services");
      categories.add("Computers");
      categories.add("Education");
      categories.add("Personal");
      categories.add("Travel");

      // Creating adapter for spinner
      dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

      // Drop down layout style - list view with radio button
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

      // attaching data adapter to spinner
      spinner.setAdapter(dataAdapter);
   }

   @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      // On selecting a spinner item
      String item = parent.getItemAtPosition(position).toString();

      // Showing selected spinner item
      Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
   }
   public void onNothingSelected(AdapterView<?> arg0) {
      // TODO Auto-generated method stub
   }
}

这或多或少是如何设置微调器和更新/添加内容。您所要做的就是调用update(add)并将您要添加的值添加到其中。

相关问题