Using Android AutoCompleteTextView with ArrayAdapter instead of ArrayAdapter

后端 未结 4 503
广开言路
广开言路 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: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:

     ArrayListarr=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....

提交回复
热议问题