2015-09-04 100 views
0

1)下面的代码:当我输入autocompletetextview时getView不会被调用。当心片段中的列表和适配器类扩展列表android arrayAdapter with autocomletetextview does not with List <...>

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)下面的代码:YES ...当我输入autocompletetextview时getView()会被调用。注意在片段中的String []数组和适配器exteding 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)选项当我在自动完成textview中键入匹配值时,不会在适配器类中调用我的getView? thx

+0

多数民众赞成它。我前几天已经修好了,你是对的。请在此评论框外回复,以便我可以查看您的答案。 thx – Al2x

+0

这不是对其他问题的评论的原因? – Luksprog

+0

nop。在另一个问题中,我有一个片段类和一个自定义适配器。当我调试适配器时,它无法找到RelativeLayout中的嵌套元素 – Al2x

回答

1

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

如果你还没有重写那个方法来返回一些有意义的东西(比如setDesc()设置的值),它将返回类似于Test @(somenumbershere)的东西,它与你输入的过滤器不匹配。所以,关键是要提供一个合适的toString()方法。

相关问题