2014-02-06 77 views
6

我的應用使用MultiColumnListViewhttps://github.com/huewu/PinterestLikeAdapterView),正如其名稱所示,它有助於創建多列列表視圖。notifyDataSetChanged不起作用

這個庫很棒,但是正如它們指定的那樣,不支持過濾。

所以我想用一個簡單的編輯文本創建我自己的文本過濾功能。

最後我實現了篩選列表,但現在我需要刷新listView並重新調用setAdapter,因爲我通過它的列表。

MyAdapter adapter = new MyAdapter(this,myList); 
listView.setAdapter(adapter); 

當我執行listView.invalidateViews()adapter.notifyDataSetChanged() ListView控件被刷新,但與舊名單。

哪個是回顧setAdapter的最好方法?也許是這樣做的另一種方式..

在此先感謝

編輯:

//Method that filters the list 
Log.i("On filter myList.size()",""+myList.size());  
adapter.notifyDataSetChanged(); 

//on the Adapter 
Log.i("On Adapter myList.size()",""+myList.size()); 

日誌:

enter image description here

適配器類別:

public class MiAdaptadorListaComercios extends BaseAdapter{ 

//Textview and Imageviews declarations 

private Context contexto; 

private List<Comercio> myList; 

public MiAdaptadorListaComercios(Context c,List<Comercio> myList){ 
    this.contexto = c; 
    this.myList = new ArrayList<Comercio>(); 
    this.myList = myList; 
} 
@Override 
public int getCount() { 
    Log.i("On Adapter myList.size()",""+listaComercios.size()); 
    return myList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return myList.get(position); 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View view =null; 

    if(convertView == null) 
    { 
     LayoutInflater inflater = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.list_cell, null); 
    } 
    else{ 
     view = convertView; 
    } 

    //set views Texteviews text,font etc... 

    return view; 
} 

}

活動課:

public class ListaComercio extends Activity { 

    private MultiColumnListView mAdapterView = null; 
    EditText searchBar; 
    private ArrayList<Comercio> myList; 
    private ArrayList<Comercio> filteredList; 
    MyAdapter adapter; 


    public ListaComercio(){ 
     myList = new ArrayList<Comercio>(); 
     filteredList = new ArrayList<Comercio>();  
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.lista_comercios); 

     mAdapterView = (MultiColumnListView) findViewById(R.id.list); 

     adapter = new MyAdapter(ListaComercio.this,myList); 

     mAdapterView.setAdapter(adapter); 


     searchBar.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
        CharSequence filer = v.getText(); 

        for(Comercio co : myList) 
        { 
         if(co.getName().contains(filer)) 
         { 
          filteredList.add(co); 
         } 
        } 

        myList = filteredList; 

        Log.i("On filter myList.size()",""+myList.size()); 

        myList.clear(); 
        adapter.notifyDataSetChanged(); 
        //mAdapterView.invalidateViews(); 

        return true; 
       } 
       return false; 
      } 
     }); 
    } 
    } 
+0

也交您的活動代碼在哪裏設置此適配器 –

+0

謝謝,我剛剛添加它@MD – Andoxko

+0

這裏是解釋https://stackoverflow.com/a/20479071/1084174 –

回答

17

只是改變你的myList並調用adapter.notifyDataSetChanged()不要每次都設置一個新的適配器。

在自定義適配器的構造函數中,請調用super以ArrayList作爲參數。

調用此:

public MiAdaptadorListaComercios(Context c,List<Comercio> myList){ 
    super(c,0,myList); 
    this.contexto = c; 
    this.myList = myList; 
} 

你可以保持適配器,因爲它是這個問題是在這一行:

myList = filteredList; 

,而不是改變基準的,你應該改變列表本身。

myList.clear(); 
myList.addAll(filteredList); 

通過這樣做,你將失去你原來的列表,我建議保持另一個列表調用編輯originalList這將有完整的列表,並通過的onCreate初始化myList中:

myList=new ArrayList(originalList); 
你每次

所以要重新設置只要致電:

myList.clear(); 
myList.addAll(originalList);  

for(Comercio co : originalList) 
      { 
       if(co.getName().contains(filer)) 
       { 
        filteredList.add(co); 
       } 
      } 
+0

謝謝你,回答這很快,但這就是我正在做的,它不工作。我編輯了我的問題以指定 – Andoxko

+0

分享您的MyAdapter課程以及 –

+0

再次感謝。但是當我在構造函數中調用'super()'時,沒有任何更改,如果我嘗試調用'super(myList)',我得到一個錯誤,告訴我必須刪除參數以匹配'BaseAdapter'。我究竟做錯了什麼? – Andoxko

5

需要清除舊的myList.clear()然後調用adapter.notifyDataSetChanged()

更新:

更改您的代碼,如:

public MiAdaptadorListaComercios(Context c,List<Comercio> myList){ 
this.contexto = c; 
this.myList = new ArrayList<Comercio>(); 
} 

而且,做這樣

myList.clear(); 
myList.addAll(originalList); 
+1

謝謝你的回答,但不幸的是它不工作..我編輯了我的問題,並且我已經包含更多細節。我希望你能猜出我做錯了什麼 – Andoxko

+0

@AndoniMuruamendiaraz看到我的更新 –

+0

但是當我這樣做的時候,列表總是空的......或者應該在新的後面添加'this.myList = myList'? – Andoxko

相關問題