简体   繁体   English

Android AutoCompleteTextView显示错误建议

[英]Android AutoCompleteTextView showing wrong suggestions

I had the AutoCompleteTextView working perfectly until I decided to use a custom adapter, that way I could customize the look of each row. 在决定使用自定义适配器之前,我让AutoCompleteTextView完美地工作了,这样我可以自定义每行的外观。 Here is what now happens: 这是现在发生的情况:

在此处输入图片说明

As you can see, the suggestion is wrong. 如您所见,该建议是错误的。 What is happening is as I type, it shows the correct number of suggestions, but the actual names of them are the first ones in the list. 所发生的是在我键入内容时,它显示了正确的建议数量 ,但是它们的实际名称是列表中的第一个。 So at this point it should show only one suggestion which is Texas A&M, but it instead just shows the first one in the list. 因此,在这一点上,它应该仅显示一个建议,即Texas A&M,但它仅显示列表中的第一个建议。 Here is how I am implementing my adapter. 这是我实现适配器的方式。

//setup filter
List<String> allCollegesList = new ArrayList<String>();
for(College c : MainActivity.collegeList)
{
    allCollegesList.add(c.getName());
}
AutoCompleteDropdownAdapter adapter = new AutoCompleteDropdownAdapter(main, R.layout.list_row_dropdown, allCollegesList);
//the old way of using the adapter, which worked fine
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(main, android.R.layout.simple_dropdown_item_1line, allCollegesList);
textView.setAdapter(adapter);

As well as my actual adapter class: 以及我实际的适配器类:

public class AutoCompleteDropdownAdapter extends ArrayAdapter<String>{

    MainActivity main;
    int rowLayout;
    List<String> allCollegesList;

    public AutoCompleteDropdownAdapter(MainActivity main, int rowLayout, List<String> allCollegesList) {
        super(main, rowLayout, allCollegesList);
        this.main = main;
        this.rowLayout = rowLayout;
        this.allCollegesList = allCollegesList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try{

            if(convertView==null){
                // inflate the layout
                LayoutInflater inflater = ((MainActivity) main).getLayoutInflater();
                convertView = inflater.inflate(rowLayout, parent, false);
            }

            // object item based on the position
            String college = allCollegesList.get(position);

            // get the TextView and then set the text (item name) and tag (item ID) values
            TextView collegeTextView = (TextView) convertView.findViewById(R.id.dropDownText);
            collegeTextView.setText(college);
            collegeTextView.setTypeface(FontManager.light);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;

    }
}

What happens here is that when the AutoCompleteTextView calls the getFilter method of the ArrayAdapter a Filter is returned, that takes care of filtering the ArrayAdapter , but not your allCollegesList . 这里发生的是,当AutoCompleteTextView调用ArrayAdaptergetFilter方法时,将返回一个Filter ,它负责过滤ArrayAdapter ,而不是对allCollegesList的过滤。 When you type your first characters, the methods of the Filter are called and ArrayAdapter has filtered elements at the first positions ( 0, 1, ... ). 键入第一个字符时,将调用Filter的方法,并且ArrayAdapter在第一个位置( 0, 1, ... )具有已过滤的元素。 However when the AutoCompleteTextView uses your implementation to get the Views. 但是,当AutoCompleteTextView使用您的实现获取视图时。 You use your list as if no filtering was done and use the first elements of the unfiltered list. 您可以像未进行任何过滤一样使用列表,并使用未过滤列表的前几个元素。

You can filter your own list too by overriding the getFilter method of your adapter. 您也可以通过覆盖适配器的getFilter方法来过滤自己的列表。 But that would be more coding than necessary. 但这将超出必要的编码范围。

You can use the ArrayAdapter 's methods and not your own list, instead: 您可以使用ArrayAdapter的方法,而不是您自己的列表,而是:

Use 采用

String college = getItem(position);

instead of 代替

String college = allCollegesList.get(position);

BTW: 顺便说一句:

you can get the context from parent too, using the getContext() method. 您也可以使用getContext()方法从parent获取上下文。 That way you can decouple the Adapter from MainActivity . 这样,您可以将Adapter与MainActivity分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM