如何在android中选择微调器值时只显示所选数据?

时间:2017-02-01 12:45:38

标签: android spinner

我有3个微调器,其中数据来自服务器,如果我们点击搜索按钮而不选择任何微调器然后在下一个活动,它应该显示所有列表(所有产品没有条件),但是如果我们根据3个微调器选择然后它应该只显示相关项目。我在点击搜索按钮时没有选择任何东西,但我没有在下一个活动中获得所选项目时获得所有项目。

这是我的搜索活动:

public class PMSearchActivity extends AppCompatActivity {

    /**
     * Id to identity READ_CONTACTS permission request.
     */
    private static final int REQUEST_READ_CONTACTS = 0;

    // UI references.
    private AutoCompleteTextView mEmailView;
    private EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;
    private ImageButton mtoolbar,pmplus;
    private Button mpigeonSearchbtn;
    private Spinner pmcountry;
    private Spinner pmstrain;
    private Spinner pmdistance;
    private TextView error_text;

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

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


        pmplus = (ImageButton) findViewById(R.id.plus);

        pmplus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PMSearchActivity.this, PMAddPigeonActivity.class);

                startActivity(intent);
                finish();
            }
        }) ;


        mtoolbar = (ImageButton) findViewById(R.id.toolbar_new);
        mtoolbar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = new Intent(PMSearchActivity.this, PMDashboardActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish(); //
                return false;
            }
        });
        mpigeonSearchbtn = (Button) findViewById(R.id.pmaddcompanybtn);
        mpigeonSearchbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptLogin();
            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);
        pmcountry = (Spinner) findViewById(R.id.sname);
        pmstrain = (Spinner) findViewById(R.id.fbcountry);
        pmdistance = (Spinner) findViewById(R.id.pstrain);
        error_text = (TextView) findViewById(R.id.error_text);

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

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

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

        adapter3 = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.txt, listItems3);
        pmcountry.setAdapter(adapter3);







        ListCountryTask listCountryTask = new ListCountryTask();
        listCountryTask.execute();
    }

    private void attemptLogin() {

        // Reset errors.
        // mEmailView.setError(null);
        //mPasswordView.setError(null);

        // Store values at the time of the login attempt.
        String PMcountry = pmcountry.getSelectedItem().toString();
        String PMstrains = pmstrain.getSelectedItem().toString();
        String PMdistance = pmdistance.getSelectedItem().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        }

        Intent intent = new Intent(PMSearchActivity.this, PMPigeonListingActivity.class);
        intent.putExtra("Country_name", PMcountry);
        intent.putExtra("Strain_name", PMstrains);
        intent.putExtra("Distance_name", PMdistance);
        startActivity(intent);
    }

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

        ArrayList<String> list;
        protected ProgressDialog progressDialog;
        ;

        ListStrainTask() {

        }

        @Override
        protected Void doInBackground(Void... params) {
            String result = "";
            try {
                list.add("-Select Strain-");
                  //  Json coding
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }

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

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
        }

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

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

        ArrayList<String> list;
        protected ProgressDialog progressDialog;
        ;

        ListDistanceTask() {

        }

     //  Json coding
        }

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

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            listItems2.addAll(list);
            adapter2.notifyDataSetChanged();
        }

        @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-");
                  //  Json coding

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

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            listItems3.addAll(list);
            adapter3.notifyDataSetChanged();
        }

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


}

这里是PiegonListing活动:

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


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


}

在Pigeon列表活动中,有一个选项,例如搜索活动,用户可以在不选择任何项目的情况下进行搜索,就像在搜索活动中一样。

1 个答案:

答案 0 :(得分:0)

为每个微调器设置侦听器

  

setOnItemSelectedListener

在onItemSelected方法中,获取变量中的选定值并将这些值传递给下一个活动。

E.g。

yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        String yourValue = yourSpinner.getSelectedItem().toString();

    }

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

    }
});