从Spinner获取值并将微调器值作为参数传递给json查询Android

时间:2016-01-18 08:46:22

标签: android json spinner android-spinner

我想要的是:

1]我有json服务:http://abcd.in/getAllUsers.php

2]我有一个用于过滤选项的微调器,即1]微调器,用于选择用户类型

3]我有完整的用户列表。

4]但是当我尝试使用微调器列表为空时

5]我想要这样的查询:http://abcd.in/getAllUsers.php?UserType=simple

我如何通过微调器实现这一目标。 当我试图获得像这样的微调器的价值时

param = "?UserType=";
        Log.d("param", param);
        url = url + param+spinnervalue;

列表视图为空。

我所做的是:

ArrayList<HashMap<String, String>> arraylist;

    ListView lv;
    ListViewAdapter adapter;
    Spinner spUserType, spOccupation;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_directory);

  // Spinner Drop down elements
        List<String> userType = new ArrayList<String>();
        userType.add("vip");
        userType.add("simple");
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, userType);

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

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

        Log.d("UserType", item.toString());*/

        param = "?UserType=";
        Log.d("param", param);
        url = url + param+item;

        JsonParser jParser = new JsonParser();
        JSONObject rootObj = jParser.getJSONFromUrl(url);

        arraylist = new ArrayList<HashMap<String, String>>();

        try {
            JSONArray jsonArray = rootObj.getJSONArray(array);

            Log.d("array", "" + jsonArray.length());

            for (int i = 0; i < jsonArray.length(); i++) {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject obj = jsonArray.getJSONObject(i);
                JSONObject job = obj.getJSONObject("User");
             /*   JSONArray jsonArray2 = rootObj.getJSONArray("User");
                JSONObject obj2 = jsonArray.getJSONObject(0);*/

                Log.d("obj", obj.toString());

                String pp2 = job.getString(pp);
                String uName2 = job.getString(uName);

                String bio1 = job.getString(bio);
                String email1 = job.getString(email);
                String time = job.getString(timeBlock);

                String flat = job.getString(flatRatePrice);


                Log.d("pp", uName2);
                // Log.d("tag",flag2.toString() );


                map.put("Profilepicture", pp2);

                map.put("Username", uName2);
                map.put("Biodata", bio1);
                map.put("Email", email1);

                map.put("Timeblock", time);
                map.put("Flatrateprice", flat);

              /*  map.put("Occupation",occupation);
                map.put("Email",email);

                Log.d("occupation",Occupation.toString());*/


                Log.d("map", map.toString());
                arraylist.add(map);


            }

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

        lv = (ListView) findViewById(R.id.listView);



        adapter = new ListViewAdapter(getApplicationContext(), arraylist);
        Log.d("list", arraylist.toString());
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

2 个答案:

答案 0 :(得分:1)

向微调器添加值。

this.arraySpinner = new String[] { "1", "2", "3", "4", "5" };
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);

然后使用on ItemSelected侦听器

s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        String items = spinner.getSelectedItem().toString();
        Log.i("Selected item : ", items);
        //use your logic here
        param = "?UserType=";
        Log.d("param", param);
        url = url + param+spinnervalue;
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {}

});

答案 1 :(得分:0)

你可以这样做:

ArrayList<String> userList = new ArrayList<>();
userList.add("simple");
userList.add("small");
ArrayAdapter<String> adapter =new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, userList);
your_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

       @Override
       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
          String spinnervalue = your_spinner.getSelectedItem().toString();
          //then use this for your purpose
          param = "?UserType=";
          Log.d("param", param);
          url = url + param+spinnervalue;
          }
       }

       @Override
         public void onNothingSelected(AdapterView<?> arg0) {
       }
});