1

我从multiautocompletetextview开始。我使用ArrayAdapter。现在,当我选择任何国家时,我想得到他们的代码和名称。我该怎么做?我需要帮忙。

这是我的代码。主要活动和班级国家

public class Country {
    String code;
    String name;

    public Country(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class MainActivity extends Activity {

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

        ArrayList<Country> countries = new ArrayList<>();
        countries.add(new Country("1", "Viet Nam"));
        countries.add(new Country("2", "Lao"));
        countries.add(new Country("3", "Campuchia"));
        countries.add(new Country("4", "America"));
        countries.add(new Country("5", "Boom Boom"));
        ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, countries);
        MultiAutoCompleteTextView multiAutoCompleteTextView = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
        multiAutoCompleteTextView.setAdapter(adapter);
        multiAutoCompleteTextView.setThreshold(1);
        multiAutoCompleteTextView.setDropDownWidth(300);
        multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    }
4

0 回答 0