在我的 android 应用程序中,我有一个数组列表,其中包含从服务器接收到的服务器数据。我需要在 arrayadapter 中使用这个 arraylist,以便在自动完成 TextView 中使用它来在用户键入字符时显示建议。我看过很多教程在 arrayadapter 中使用字符串数组并将其设置为自动完成 TextView 。但是还没有找到任何使用 arraylist 的解决方案。请帮帮我..
编辑:
这是我用过的代码:
View rootView = inflater.inflate(R.layout.fragment_community, container, false);
actv1=(AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);
actv2=(AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView2);
loc=new ArrayList<String>();
SplashScreen ss = new SplashScreen();
loc=ss.loc;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,loc);
添加 loc 在 logcat 中显示错误,如下所示:构造函数 ArrayAdapter(CommunityFragment, int, ArrayList) 未定义
最佳答案
来自ArrayAdapter documentation ,
public ArrayAdapter (**Context context**, int resource, List<T> objects)
从你的错误来看,你似乎传递了一个 Fragment 引用,它不是上下文。
尝试
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,loc);
关于android - 使用 arraylist<string> 为 android 中的自动完成 TextView 设置适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25346280/