没有在android中显示来自微调器的选定项目?

时间:2017-02-02 04:58:40

标签: android json android-spinner

我有3个微调器,如果我选择它应该显示与该选择相关的项目。但它显示来自服务器的所有项目而不是选择项目。 在getdata()代码中的方法我得到的所有项目没有选择。 在filter()方法中,我只需显示所选项目,但仍显示所有项目。

这是我的代码:

    public class PMPigeonListingActivity extends AppCompatActivity {

    private Button mpigeonListBtn;
    private ImageView mimg3;
    private ImageButton mtoolbar;
    private String PostCountry;
    private String PostStrain;
    private String PostDistance;
    private Button listpigeonbutton;
    private Spinner lsDistance;
    private Spinner lsStrain;
    private Spinner lsCountry;
    private Button lssearchbutton;
    private TextView listallbtn;
    //Web api url

    // distance part
    ArrayList<String> listItems = new ArrayList<>();
    ArrayAdapter<String> adapter;

    ArrayList<String> listItems2 = new ArrayList<>();
    ArrayAdapter<String> adapter2;

    // distance part
    ArrayList<String> listItems3 = new ArrayList<>();
    ArrayAdapter<String> adapter3;

    //Tag values to read from json
    public static final String TAG_IMAGE_URL = "pimage";
    public static final String TAG_NAME = "pprice";
    public static final String TAG_PID = "pid";
    public static final String TAG_PNAME = "pname";
    public static final String TAG_PDETAILS = "pdetails";
    public static final String TAG_MOBILE = "pmobile";
    public static final String TAG_EMAIL = "pemail";


    //GridView Object
    private GridView gridView;
    private GridView gridView2;

    //ArrayList for Storing image urls and titles
    private ArrayList<String> images;
    private ArrayList<String> names;
    private ArrayList<Integer> pid;
    private ArrayList<String> pname;
    private ArrayList<String> pdetails;
    private ArrayList<String> pimage;
    private ArrayList<String> pmobile;
    private ArrayList<String> pemail;

    //for inline search
    private ArrayList<String> images2;
    private ArrayList<String> names2;
    private ArrayList<Integer> pid2;
    private ArrayList<String> pname2;
    private ArrayList<String> pdetails2;
    private ArrayList<String> pimage2;
    private ArrayList<String> pmobile2;
    private ArrayList<String> pemail2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pmpigeon_listing);
        getSupportActionBar().hide();
        gridView = (GridView) findViewById(R.id.gridView);
     //   gridView2 = (GridView) findViewById(R.id.gridView);


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            PostCountry = extras.getString("Country_name");
            PostStrain = extras.getString("Strain_name");
            PostDistance = extras.getString("Distance_name");
        }

        images = new ArrayList<>();
        names = new ArrayList<>();
        pid = new ArrayList<>();
        pname = new ArrayList<>();
        pdetails = new ArrayList<>();
        pmobile = new ArrayList<>();
        pemail = new ArrayList<>();
        images2 = new ArrayList<>();
        names2 = new ArrayList<>();
        pid2 = new ArrayList<>();
        pname2 = new ArrayList<>();
        pdetails2 = new ArrayList<>();
        pmobile2 = new ArrayList<>();
        pemail2 = new ArrayList<>();
        lsStrain = (Spinner) findViewById(R.id.lsStrain);
        lsDistance = (Spinner) findViewById(R.id.lsDistance);
        lsCountry = (Spinner) findViewById(R.id.lsCountry);
        lssearchbutton = (Button) findViewById(R.id.lssearchbutton);
        listallbtn = (TextView) findViewById(R.id.listallbtn);

        if (PostCountry.equals("Select Country") && PostStrain.equals("Select Strain") && PostDistance.equals("Select Distance")) {
            listallbtn.setVisibility(View.GONE);
        } else {
            listallbtn.setVisibility(View.VISIBLE);
        }
        //Calling the getData method
        getData();
        mtoolbar = (ImageButton) findViewById(R.id.toolbar_new);
        mtoolbar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = new Intent(PMPigeonListingActivity.this, PMDashboardActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish(); //
                return false;
            }
        });

        lssearchbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lsCountry.getSelectedItemPosition() != 0 || lsStrain.getSelectedItemPosition() != 0 || lsDistance.getSelectedItemPosition() != 0) {
                    listallbtn.setVisibility(View.VISIBLE);
                } else {
                    listallbtn.setVisibility(View.GONE);
                }
                images2.clear();
                names2.clear();
                pid2.clear();
                pname2.clear();
                pdetails2.clear();
                pmobile2.clear();
                pemail2.clear();
                filter();
            }
        });

        listallbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lsCountry.getSelectedItemPosition() != 0 || lsStrain.getSelectedItemPosition() != 0 || lsDistance.getSelectedItemPosition() != 0) {
                    listallbtn.setVisibility(View.VISIBLE);
                } else {
                    listallbtn.setVisibility(View.GONE);
                }
                lsCountry.setSelection(0);
                lsStrain.setSelection(0);
                lsDistance.setSelection(0);
                images2.clear();
                names2.clear();
                pid2.clear();
                pname2.clear();
                pdetails2.clear();
                pmobile2.clear();
                pemail2.clear();
                filter();
            }
        });

        // button list
        listpigeonbutton = (Button) findViewById(R.id.listpigeonbutton);
        listpigeonbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PMPigeonListingActivity.this, PMAddPigeonActivity.class);
                startActivity(intent);
            }
        });

        adapter = new ArrayAdapter<String>(this, R.layout.spinner_small, R.id.txt, listItems);
        lsStrain.setAdapter(adapter);

        ListDistanceTask distanceTask = new ListDistanceTask();
        distanceTask.execute();

        adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_small, R.id.txt, listItems2);
        lsDistance.setAdapter(adapter2);
        ListStrainTask strainTask = new ListStrainTask();
        strainTask.execute();

        adapter3 = new ArrayAdapter<String>(this, R.layout.spinner_small, R.id.txt, listItems3);
        lsCountry.setAdapter(adapter3);
        ListCountryTask listCountryTask = new ListCountryTask();
        listCountryTask.execute();


    }

    private void getData() {
        //Showing a progress dialog while our app fetches the data from url
        final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
        String DATA_URL = "http://......searchPigeonList";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show();
                        loading.dismiss();
                        try {
                            JSONArray json = new JSONObject(response).getJSONArray("pigeon_list");
                            for (int i = 0; i < json.length(); i++) {
                                JSONObject obj = null;
                                try {
                                    obj = json.getJSONObject(i);
                                    pid.add(obj.getInt("id"));
                                    pname.add(obj.getString("pigeon_name"));

                                    pdetails.add(obj.getString("pigeon_details"));
                                    pmobile.add(obj.getString("usr_mobile"));
                                    pemail.add(obj.getString("usr_email"));
                                    images.add(obj.getString("image"));
                                    names.add(obj.getString("pigeon_price"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }catch(JSONException je){
                            je.printStackTrace();
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                        Log.d("Test",response);
                        //Creating GridViewAdapter Object
                        PMPigeonListAdapter pmpigeonlistadapter = new PMPigeonListAdapter(getApplicationContext(), images, names, pid, pdetails, pmobile, pemail, pname);


                        //Adding adapter to gridview
                        gridView.setAdapter(pmpigeonlistadapter);
                        pmpigeonlistadapter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //Toast.makeText(PMPigeonListingActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("country", PostCountry);
                params.put("strain", PostStrain);
                params.put("distance", PostDistance);
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }

    private void filter() {
        //Showing a progress dialog while our app fetches the data from url
        final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false);
        String DATA_URL = "http://......hPigeonList";
        final String lstrain = lsStrain.getSelectedItem().toString();
        final String ldistance = lsDistance.getSelectedItem().toString();
        final String lcountry = lsCountry.getSelectedItem().toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show();
                        loading.dismiss();
                        try {
                            JSONArray json = new JSONObject(response).getJSONArray("pigeon_list");
                            for (int i = 0; i < json.length(); i++) {
                                JSONObject obj = null;
                                try {
                                    obj = json.getJSONObject(i);
                                    pid2.add(obj.getInt("id"));
                                    pname2.add(obj.getString("pigeon_name"));

                                    pdetails2.add(obj.getString("pigeon_details"));
                                    pmobile2.add(obj.getString("usr_mobile"));
                                    pemail2.add(obj.getString("usr_email"));
                                    images2.add(obj.getString("image"));
                                    names2.add(obj.getString("pigeon_price"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        //Creating GridViewAdapter Object
                        PMPigeonSearchInlineAdapter pmPigeonSearchInlineAdapter = new PMPigeonSearchInlineAdapter(getApplicationContext(), images2, names2, pid2, pdetails2, pmobile2, pemail2, pname2);



                    //Adding adapter to gridview
                   // pmPigeonSearchInlineAdapter.notifyDataSetChanged();
                   // gridView2.setAdapter(pmPigeonSearchInlineAdapter);
                    //Log.d("TAG",gridView2.getAdapter().getClass().getName());


                    pmPigeonSearchInlineAdapter.notifyDataSetChanged();
                    gridView.setAdapter(pmPigeonSearchInlineAdapter);
                    Log.d("TAG",gridView.getAdapter().getClass().getName());

                    Toast.makeText(PMPigeonListingActivity.this, ""+gridView.getAdapter().getClass().getName() , Toast.LENGTH_SHORT).show();




                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(PMPigeonListingActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params2 = new HashMap<String, String>();
                params2.put("country", lcountry);
                params2.put("strain", lstrain);
                params2.put("distance", ldistance);
                return params2;
            }

        };

        RequestQueue requestQueue2 = Volley.newRequestQueue(this);
        gridView2.setAdapter(null);
        requestQueue2.add(stringRequest);


    }

    public class ListStrainTask extends AsyncTask<Void, Void, Void> {

        //  some coding

    }

    // listdistancetask
    public class ListDistanceTask extends AsyncTask<Void, Void, Void> {

        //  some coding

        }

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            listItems2.addAll(list);
            adapter2.notifyDataSetChanged();
            ArrayAdapter<String> array_spinner = (ArrayAdapter<String>) lsDistance.getAdapter();
            lsDistance.setSelection(array_spinner.getPosition(PostDistance));
        }

        @Override
        protected void onCancelled() {
            // ml = null;
            progressDialog.dismiss();
        }
    }

    public class ListCountryTask extends AsyncTask<Void, Void, Void> {

        ArrayList<String> list;
        protected ProgressDialog progressDialog;
        ;

        ListCountryTask() {

        }

        @Override
        protected Void doInBackground(Void... params) {
            String result = "";
            try {
                list.add("Select Country");
              //  some coding

                }





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

            return null;

        }

        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(PMPigeonListingActivity.this, "Please wait...", "Fetching data", true, false);
            list = new ArrayList<>();
        }

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            listItems3.addAll(list);
            adapter3.notifyDataSetChanged();
            ArrayAdapter<String> array_spinner = (ArrayAdapter<String>) lsCountry.getAdapter();
            lsCountry.setSelection(array_spinner.getPosition(PostCountry));
        }

        @Override
        protected void onCancelled() {
            // ml = null;
            progressDialog.dismiss();
        }
    }


}

这是我的json输出:

{
  "status_code": 200,
  "status": "OK",
  "status_message": "Success",
  "pigeon_list": [
    {
      "id": "1",
      "pigeon_name": "sofiee",
      "auth_token": "58809c7129a5a",
      "country_code": "AE",
      "strain_id": "75",
      "distance": "3",
      "pigeon_price": "50.00",
      "pigeon_details": "One of the best ",
      "image": "http:.98a8ac5.jpeg",
      "pedigree_image": "http://...1.jpeg",...
      "status": "",
      "created": "2017-01-19 16:52:14",
      "updated": "0000-00-00 00:00:00",
      "strain_name": "Janssen/gaston wowers ",
      "usr_mobile": "+971/505040009",
      "usr_image": "http://....19a.jpeg",
      "usr_email": "...edo@gmail.com"
    },

2 个答案:

答案 0 :(得分:1)

您没有更改适配器数据。 试试这段代码。

 @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            listItems3.addAll(list);
            adapter3.clear();
            adapter3.addAll(listItems3);
            adapter3.notifyDataSetChanged();
            ArrayAdapter<String> array_spinner = (ArrayAdapter<String>) lsCountry.getAdapter();
            lsCountry.setSelection(array_spinner.getPosition(PostCountry));
        }

答案 1 :(得分:0)

您可以Log我认为你应该做的帖子参数:

final String lstrain = (String)lsStrain.getSelectedItem();