示例代码如下:
public class CustomAdapter extends ArrayAdapter
public CustomAdapter(Context context, int resource, List items) {
super(context, resource, items);
mContext = context;
mResource = resource;
mItems = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(mItems.get(position));
if (mItems.get(position).contains("background color")) {
convertView.setBackgroundColor(Color.YELLOW);
} else {
convertView.setBackgroundColor(Color.WHITE);
}
return convertView;
}
}
示例代码如下:
AutoCompleteTextView textView = findViewById(R.id.autoCompleteTextView);
List
CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_list_item_1, items); textView.setAdapter(adapter);
通过设置自定义Adapter,从而可以在getView中更新需要改变的背景,最终解决AutoCompleteText无法更新弹出窗口背景的问题。