【问题标题】:Using arraylist<string> for setting adapter for autocomplete textview in android使用 arraylist<string> 为android中的自动完成文本视图设置适配器
【发布时间】:2014-10-10 08:40:36
【问题描述】:

在我的 android 应用程序中,我有一个数组列表,其中包含从服务器接收到的服务器数据。我需要在 arrayadapter 中使用这个 arraylist,以便在自动完成文本视图中使用它,以便在用户键入字符时显示建议。我已经看到很多教程在 arrayadapter 中使用字符串数组并将其设置为自动完成文本视图。但是还没有找到任何使用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) is undefined

【问题讨论】:

  • 显示您尝试完成但不起作用的代码。是什么导致了问题?
  • 查看我的编辑@ChiefTwoPencils。
  • ArrayAdapter&lt;String&gt;更改为ArrayAdapter&lt;ArrayList&lt;String&gt;&gt;
  • @mmlooloo。它在 Eclipse 中显示如下错误:构造函数 ArrayAdapter>(CommunityFragment, int, ArrayList) is undefined
  • @Tee Jay 删除我的建议并使用你的旧

标签: android arrays arraylist autocompletetextview


【解决方案1】:

来自ArrayAdapter documentation

public ArrayAdapter (**Context context**, int resource, List<T> objects)

从您的错误来看,您似乎传递了一个片段引用,这不是上下文。

试试

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,loc);

【讨论】:

  • 不工作。像这样在 ecrise 中显示错误。构造函数 ArrayAdapter>(FragmentActivity, int, ArrayList) 未定义
  • 我认为这不是上下文的问题。我需要在我的代码中向 arrayadapter 添加一个 arraylist 而不是 array..
  • @Tee Jay 删除我的建议并使用你的旧
  • @TeeJay 是。 this 不是有效的上下文。片段不扩展上下文活动,你需要getActivity()
  • @Tee Jay 我弄错了,如果你只使用上面的答案,你会得到结果。对不起!!
猜你喜欢
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多