notifyDataSetChanged不起作用

时间:2014-02-06 09:24:16

标签: android listview adapter

我的应用使用MultiColumnListViewhttps://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());

日志:

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;
            }
        });
    }
    }

2 个答案:

答案 0 :(得分:17)

只需更改myList并致电adapter.notifyDataSetChanged(),不要每次都设置新适配器。

在自定义适配器的构造函数中,调用以ArrayList作为参数的super

打电话给:

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);
                }
            }

答案 1 :(得分: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);   
相关问题