我的應用使用MultiColumnListView
(https://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());
日誌:
適配器類別:
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;
}
});
}
}
也交您的活動代碼在哪裏設置此適配器 –
謝謝,我剛剛添加它@MD – Andoxko
這裏是解釋https://stackoverflow.com/a/20479071/1084174 –