2015-09-04 101 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()方法。

相關問題