我的应用程序使用了一个MultiColumnListView (https://github.com/huewu/PinterestLikeAdapterView),它的名字表示它有助于创建多列列表视图。
库很好,但正如他们所指定的,不支持过滤。
所以我尝试用一个简单的编辑文本来创建我自己的文本过滤功能。
最后,我实现了对列表的过滤,但是现在我需要刷新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;
}
});
}
}发布于 2014-02-06 09:25:39
只需更改您的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);通过这样做,您将把原来的列表松开,我建议保留另一个列表调用ed originalList,该列表将具有完整的列表,并通过以下方式在onCreate中初始化myList:
myList=new ArrayList(originalList);所以每次你想重新设置时只要打电话:
myList.clear();
myList.addAll(originalList); 和在
for(Comercio co : originalList)
{
if(co.getName().contains(filer))
{
filteredList.add(co);
}
}发布于 2014-02-06 09:31:48
您需要清除旧的myList.clear(),然后调用adapter.notifyDataSetChanged()
更新:
更改代码如下:
public MiAdaptadorListaComercios(Context c,List<Comercio> myList){
this.contexto = c;
this.myList = new ArrayList<Comercio>();
}还有,就像
myList.clear();
myList.addAll(originalList); https://stackoverflow.com/questions/21598621
复制相似问题