Using Android AutoCompleteTextView with ArrayAdapter instead of ArrayAdapter

后端 未结 4 499
广开言路
广开言路 2020-12-23 17:51

I wanted to use AutoCompleteTextView in my android application.I know how to use it with simple array of Strings, but I wanted AutoCompleteTextView to use list of Objects t

相关标签:
4条回答
  • 2020-12-23 18:21

    You can override getItem in this adapter, depend on each type to convert it into String

     override fun getItem(position: Int): String? {
            val item = listData[position]
            when (item) {
                is BankItem -> {
                    return item.bankName
                }
            ....
     }
    
    0 讨论(0)
  • 2020-12-23 18:24

    if you are wanna add the data in String[] arr=new String[100]; then its wrong. You can do the same work as in form of ArrayList but remember you will never put here a Getter/Setter class. Just simply declare it. See this example.

    Declare in main partition:

     ArrayList<String>arr=new ArrayList<>();
    

    and then initilize it in this way:

     for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                        String imei = jsonObject1.getString("imei");
                        String name = jsonObject1.getString("name");
                        String my_pic = jsonObject1.getString("my_pic");
                        String email = jsonObject1.getString("email");
    
                        arr.add(name);
                    }
    
    
                    adapter= new ArrayAdapter<>
                            (this, android.R.layout.select_dialog_item, arr);
                    autoCompleteText.setThreshold(1);//will start working from first character
                    autoCompleteText.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
                    autoCompleteText.setTextColor(Color.RED);
    
    
                }
    

    I hope this will work for you....

    0 讨论(0)
  • 2020-12-23 18:42

    You can use an AbstractList to get the String representation of each item in your object list.

    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));
    }
    
    0 讨论(0)
  • 2020-12-23 18:43

    Two ways:

    1. Override toString() in Student class and make it return name. You can get the object that was selected with the following code:

       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. Implement a custom adapter (by extending BaseAdapter class or ArrayAdapter<Student> class) Check this tutorial : http://www.ezzylearning.com/tutorial.aspx?tid=1763429

    0 讨论(0)
提交回复
热议问题