【问题标题】:android arrayAdapter with autocomletetextview not working with List<...>带有 autocomletetextview 的 android arrayAdapter 不适用于 List<...>
【发布时间】:2015-09-04 15:27:12
【问题描述】:

1) 下面的代码:当我输入 autocompletetextview 时,getView 不会被调用。注意片段中的列表和扩展列表的适配器类

   public class SigninFragment extends Fragment {
        private List<Test> list= null;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Test tes= new Test ();
    tes.setId(1);
    tes.setDesc("descabc");
    list= new ArrayList<>();
    list.add(prof);

    tesListAdapter =
            new TesListAdapter(
                    rootView.getContext()
                    ,R.layout.list_row_adapter
                    ,list);

    autocompletetextview.setThreshold(3);
    autocompletetextview.setAdapter(tesListAdapter );

我的适配器类:

public class ProfissoesListAdapter extends ArrayAdapter<Test> { **<==HERE**
    private LayoutInflater inflater;
    private int resource;

public TesListAdapter(Context activity, int resource, List<Test> listaProf) **<==HERE**{

    super(activity, resource, listaProf);
    this.inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.resource = resource;
}

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

...

2) 下面的代码:是的... getView() 确实在我输入 autocompletetextview 时被调用。注意片段中的 String[] 数组和扩展 ArrayAdapter 的适配器

   public class SigninFragment extends Fragment {
        private List<Test> list= null;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    String[] list =  {"abcde","bbbbbb","bbbakaka","ccccccc","dddddd"};

    tesListAdapter =
            new TesListAdapter(
                    rootView.getContext()
                    ,R.layout.list_row_adapter
                    ,list);

    autocompletetextview.setThreshold(3);
    autocompletetextview.setAdapter(tesListAdapter );

我的适配器类:

public class ProfissoesListAdapter extends ArrayAdapter<String> {  **<==HERE**
    private LayoutInflater inflater;
    private int resource;

public TesListAdapter(Context activity, int resource, String[] listaProf) {  **<==HERE**

    super(activity, resource, listaProf);
    this.inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.resource = resource;
}

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

...

问题:当我在自动完成文本视图中键入匹配值时,为什么 1) 选项不会在适配器类中调用我的 getView?谢谢

【问题讨论】:

  • 就是这样。我几天前已经修好了,你是对的。请在此评论框外回复,以便我检查您的答案。谢谢
  • 这不是对其他问题发表评论的原因吗?
  • 没有。在另一个问题中,我有一个片段类和一个自定义适配器。当我调试适配器时,它无法在 RelativeLayout 中找到嵌套元素
  • @Luksprog 如果可能的话,请你看看我的问题stackoverflow.com/questions/32722886/…

标签: android


【解决方案1】:

在第一种情况下,不会调用 getView(),因为它与您在自动完成小部件中键入的内容不匹配。问题是,当与自定义类(不是 String/CharSequence)一起使用时,ArrayAdapter 通过在数据对象上调用 toString() 来获取数据(在您的情况下是 Test 的 toString() 方法)。

如果您没有重写该方法以返回有意义的内容(例如使用 setDesc() 设置的值),它将返回类似 Test@(somenumbershere) 的内容,该内容与您作为过滤器键入的内容不匹配。所以,关键是要提供一个合适的 toString() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多