对话框中具有动态值的自动完成TextView

时间:2019-07-01 05:51:52

标签: android autocomplete dialog spinner android-alertdialog

我正在使用自动完成的textview从服务器动态检索数据。此自动完成的textview在自定义对话框中。一切正常,但唯一的问题是,当我尝试检索选定的自动完成值时,它给了我null值。

这是我在片段中的声明-

AutoCompleteTextView sp_city;
ArrayAdapter<GetMyCityAdapter> spinneradapter;

    List<GetMyCityAdapter> getMyCityAdapter1;
    private ArrayList<String> city = new ArrayList<String>();

这是我的对话框代码-

 btn_searchfreelancerfilter.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
                // ...Irrelevant code for customizing the buttons and title
                LayoutInflater inflater = getActivity().getLayoutInflater();
                final View dialogView = inflater.inflate(R.layout.freelancerfilterdialog, null);
                dialogBuilder.setView(dialogView);
                dialogBuilder.setCancelable(true);
                dialogBuilder.setTitle("Search Freelancer");
                sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        selectedcityvalue = spinneradapter.getItem(i).getCity_name();
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });



                sp_city = (AutoCompleteTextView) dialogView.findViewById(R.id.sp_sf_city);
                      loadCity();

                dialogBuilder.setPositiveButton("Search", new DialogInterface.OnClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                    public void onClick(DialogInterface dialog, int id) {

                        LoadFilteredData(selectedcityvalue);

                    }
                });
                final AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();

            }
        });

这是我的loadCity方法代码-

  private void loadCity() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CITY, new Response.Listener<String>() {
            @Override
            public void onResponse(String response_city) {
                showJSONCITY(response_city);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("Error", "" + error);
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }
    private void showJSONCITY(String response_city) {
        try {
            JSONObject jsonObject = new JSONObject(response_city);
            JSONArray data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject1 = data.getJSONObject(i);
                GetMyCityAdapter getMyCityAdapter2 = new GetMyCityAdapter("0", "select_type");
                getMyCityAdapter2.setCity_id(jsonObject1.getString("c_id"));
                getMyCityAdapter2.setCity_name(jsonObject1.getString("c_name"));
                getMyCityAdapter1.add(getMyCityAdapter2);
                city.add(jsonObject1.getString("c_name"));
            }

            ArrayList<GetMyCityAdapter> citylist = new ArrayList<>();
            for (int i = 0; i < city.size(); i++) {
                citylist.add(new GetMyCityAdapter(getMyCityAdapter1.get(i).getCity_id(), getMyCityAdapter1.get(i).getCity_name()));
            }
            spinneradapter = new ArrayAdapter<GetMyCityAdapter>(getActivity(), android.R.layout.simple_spinner_dropdown_item, citylist);
            sp_city.setAdapter(spinneradapter);
            sp_city.setThreshold(1);
            sp_city.setSelection(0);

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

我已成功检索到数据。自动完成已使用服务器中的数据填充。只是在setitemclicklistener上未检索到。 没有错误,但是在检索所选值时会给出空值。

1 个答案:

答案 0 :(得分:0)

您应该先致电

  

loadCity();

就像:

 sp_city = (AutoCompleteTextView) dialogView.findViewById(R.id.sp_sf_city);
                  loadCity();

    LayoutInflater inflater = getActivity().getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.freelancerfilterdialog, null);
            dialogBuilder.setView(dialogView);
            dialogBuilder.setCancelable(true);
            dialogBuilder.setTitle("Search Freelancer");
            sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    selectedcityvalue = spinneradapter.getItem(i).getCity_name();
                }
                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });

通过这种方式,您需要先从服务器调用所有数据,然后再将适配器设置为AutoCompleteTextView。

让我知道它是否有效。