AutoCompleteTextView不显示下拉项

时间:2018-07-30 13:52:27

标签: java android autocompletetextview

我在显示城市的下拉列表时遇到问题。 我正在使用自动完成TextView显示下拉列表。 该应用程序的工作方式如下:

  • 首先,您需要选择一个省,然后该省内的所有城市都会显示在城市的下拉列表中 但相反,它只显示了一个空的下拉列表。
  • 用户选择一个省后,它将显示一个文本,即选定的省
  • 之后,您需要选择城市
  • 然后我将city_picker.setText设置为空字符串 因为我不希望用户选择不在该省内的城市。

代码:

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView province_picker, city_picker;
    public static final String TAG = "Locationdemo";
    private ArrayList<String> province = new ArrayList<>();
    private ArrayList<String> city = new ArrayList<>();
    private ArrayAdapter<String> province_adapter, city_adapter;
    private String province_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        city_picker = findViewById(R.id.city_picker);
        city_adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, city);
        city_picker.setAdapter(city_adapter);

        city_picker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View arg0) {
                city_picker.showDropDown();
                getCurrentFocus().clearFocus();
            }
        });

        city_picker.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                city_picker.setError(null, null);
            }
        });

        province_picker = findViewById(R.id.province_picker);
        province_adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, province);
        province_picker.setAdapter(province_adapter);
        province_picker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View arg0) {
                getProvince();
                province_picker.showDropDown();
                getCurrentFocus().clearFocus();
            }
        });

        province_picker.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                city_picker.setText("");
                String item_selected = (String) parent.getItemAtPosition(position);
                province_picker.setError(null, null);
                getProvinceCode(item_selected);
                getCity();
                //Log.i(TAG, item_selected);
            }
        });
    }

    /**
    public void nextButton(View view) {
        String province = province_picker.getText().toString();
        String city = city_picker.getText().toString();
        if (validateInputs(province, city)) {

        }
    }

    private boolean validateInputs(String province, String city) {
        if (province.equals("")) {
            province_picker.setError("Required");
        }
        if (city.equals("")) {
            city_picker.setError("Required");

        }
        return true;
    }
    **/

    public void getCity() {
        city.clear();
        String city_json;

        try {
            InputStream input_stream = getAssets().open("city.json");
            int size = input_stream.available();
            byte[] buffer = new byte[size];
            input_stream.read(buffer);
            input_stream.close();
            city_json = new String(buffer, "UTF-8");
            JSONArray json_array = new JSONArray(city_json);

            for (int i = 0; i < json_array.length(); i++) {
                JSONObject json_object = json_array.getJSONObject(i);
                if (json_object.getString("provCode").equals(province_code)) {
                    city.add(json_object.getString("citymunDesc"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.i(TAG, city.toString());
        Log.i(TAG, "City Length: " + city.size());

        city_adapter.getFilter().filter(null);
        city_adapter.notifyDataSetChanged();
    }

    public void getProvinceCode(String province) {
        String province_json;

        try {
            InputStream input_stream = getAssets().open("province.json");
            int size = input_stream.available();
            byte[] buffer = new byte[size];
            input_stream.read(buffer);
            input_stream.close();
            province_json = new String(buffer, "UTF-8");
            JSONArray json_array = new JSONArray(province_json);

            for (int i = 0; i < json_array.length(); i++) {
                JSONObject json_object = json_array.getJSONObject(i);

                if (json_object.getString("provDesc").equals(province)) {
                    province_code = json_object.getString("provCode");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //Log.i(TAG, province_code);
    }

    public void getProvince() {
        //Log.i(TAG, "getProvince()");
        //Log.i(TAG, "Province: " + province.toString());

        province.clear();
        String province_json;

        try {
            InputStream input_stream = getAssets().open("province.json");
            int size = input_stream.available();
            byte[] buffer = new byte[size];
            input_stream.read(buffer);
            input_stream.close();
            province_json = new String(buffer, "UTF-8");
            JSONArray json_array = new JSONArray(province_json);

            for (int i = 0; i < json_array.length(); i++) {
                JSONObject json_object = json_array.getJSONObject(i);
                province.add(json_object.getString("provDesc"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //Log.i(TAG, province.toString());
        //Log.i(TAG, "Province Length: " + province.size());
    }
}

布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/province_input_layout"
        style="@style/TextInputLayoutStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:hint="@string/province"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <AutoCompleteTextView
            android:id="@+id/province_picker"
            style="@style/AutoCompleteTextViewStyle.Picker"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/city_input_layout"
        style="@style/TextInputLayoutStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:hint="@string/city"
        app:layout_constraintEnd_toEndOf="@+id/province_input_layout"
        app:layout_constraintStart_toStartOf="@+id/province_input_layout"
        app:layout_constraintTop_toBottomOf="@+id/province_input_layout">

        <AutoCompleteTextView
            android:id="@+id/city_picker"
            style="@style/AutoCompleteTextViewStyle.Picker"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/next_button"
        style="@style/ButtonStyle.Light"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:onClick="nextButton"
        android:text="@string/next"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/city_input_layout" />

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案