Używanie Android AutoCompleteTextView z ArrayAdapter zamiast ArrayAdapter

Chciałem użyć AutoCompleteTextView w mojej aplikacji na Androida.Wiem, jak używać go z prostą tablicą łańcuchów, ale chciałem AutoCompleteTextView używać listy obiektów do wykonania completion.My kod dla tego jest następujący:

KOD AKTYWNOŚCI

public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        initialize();
        ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this,
                R.layout.dropdown_list_item, GetAllStudentsList());

        searchBox.setAdapter(adapter);
        searchBox.setThreshold(THRESHOLD_VALUE);
        searchBox.setTextColor(Color.BLACK);
}

searchBox.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                int position, long arg3) {
                     //Here i will grab the Student object that user selected from drop-down

        }

    });

public ArrayList<Movies> GetAllStudentsList() {

//This method returns a ArrayList of <Student> type objects
}

Obiekt klasy Student zawiera informacje dotyczące ucznia, którym jest ID,NAME,ADDRESS,MARKS.

Wiem, że AutoCompleteTextView potrzebuje tablicy obiektu typu String do wykonania wyszukiwania operation.In moja sprawa chcę AutoCompleteTextView, aby użyć mojej ArrayList do wykonania uzupełnienia na podstawie nazwy pola obiektu studenta.Nie wiem, jak określić AutoCompleteTextView, aby użyć pola nazwa obiektu Student.Proszę o pomoc w podaniu linku lub małego przykładu.

Dzięki

Author: Anshul, 2012-10-25

2 answers

Dwa sposoby:

  1. Override toString() in Student class and make it return name. Możesz uzyskać obiekt, który został wybrany za pomocą następującego kodu:

     public static class Student {
    
        private String name;
    
            public Student(String name) {
                this.name = name;
            }
    
            @Override
            public String toString() {
                return name;
            }
    
        }
    
    AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    final ArrayList<Student> list = new ArrayList<MainActivity.Student>();
    list.add(new Student("Viru"));
    list.add(new Student("Gauti"));
    ArrayAdapter<Student> adapter = new ArrayAdapter<MainActivity.Student>(
            this, android.R.layout.simple_dropdown_item_1line, list);
    tv.setAdapter(adapter);
    
    tv.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Student selected = (Student) arg0.getAdapter().getItem(arg2);
            Toast.makeText(MainActivity.this,
                    "Clicked " + arg2 + " name: " + selected.name,
                    Toast.LENGTH_SHORT).show();
        }
    });
    
  2. Zaimplementuj Niestandardowy adapter (rozszerzając klasę BaseAdapter lub klasę ArrayAdapter<Student>) Sprawdź ten samouczek: http://www.ezzylearning.com/tutorial.aspx?tid=1763429

 66
Author: Sameer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-15 10:04:14

Możesz użyć AbstractList, Aby uzyskać reprezentację String każdego elementu na liście obiektów.

private void setupAutoComplete(AutoCompleteTextView view, List<T> objects) {
    List<String> names = new AbstractList<String>() {
        @Override
        public int size() { return objects.size(); }

        @Override
        public String get(int location) {
            return objects.get(location).getName();
        }
    };

    view.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
}
 4
Author: Italo Borssatto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-14 22:12:56