And*_*xko 7 android listview adapter
我的应用程序使用MultiColumnListView(https://github.com/huewu/PinterestLikeAdapterView),正如其名称所示,它有助于创建多列列表视图.
该库很棒,但正如它们指定的那样,不支持过滤.
因此,我尝试使用简单的编辑文本创建自己的文本过滤器功能.
最后我实现了过滤列表,但现在我需要刷新listView并调用setAdapter,因为我传递了列表.
MyAdapter adapter = new MyAdapter(this,myList);
listView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
当我执行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());
Run Code Online (Sandbox Code Playgroud)
日志:

适配器类:
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;
}
Run Code Online (Sandbox Code Playgroud)
}
活动类:
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;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
vip*_*tal 17
只需更改你的myList电话adapter.notifyDataSetChanged(),每次都不要设置新的适配器.
在自定义适配器的构造函数中,执行super以ArrayList作为参数的调用.
叫这个:
public MiAdaptadorListaComercios(Context c,List<Comercio> myList){
super(c,0,myList);
this.contexto = c;
this.myList = myList;
}
Run Code Online (Sandbox Code Playgroud)
您可以保留适配器,因为问题在于此行:
myList = filteredList;
Run Code Online (Sandbox Code Playgroud)
而不是更改引用,您应该更改列表本身.
myList.clear();
myList.addAll(filteredList);
Run Code Online (Sandbox Code Playgroud)
通过执行此操作,您将丢失原始列表,我建议保留另一个列表调用ed originalList,它将具有完整列表并在onCreate中初始化myList:
myList=new ArrayList(originalList);
Run Code Online (Sandbox Code Playgroud)
所以每次你想重新设置只是打电话:
myList.clear();
myList.addAll(originalList);
Run Code Online (Sandbox Code Playgroud)
并在
for(Comercio co : originalList)
{
if(co.getName().contains(filer))
{
filteredList.add(co);
}
}
Run Code Online (Sandbox Code Playgroud)
你需要清除旧的myList.clear()然后再打电话adapter.notifyDataSetChanged()
更新:
更改您的代码,如:
public MiAdaptadorListaComercios(Context c,List<Comercio> myList){
this.contexto = c;
this.myList = new ArrayList<Comercio>();
}
Run Code Online (Sandbox Code Playgroud)
还有,喜欢
myList.clear();
myList.addAll(originalList);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27424 次 |
| 最近记录: |