Android notifyDataSetChanged 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21598621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:00  来源:igfitidea点击:

notifyDataSetChanged not working

androidlistviewadapter

提问by Andoxko

My app uses a MultiColumnListView(https://github.com/huewu/PinterestLikeAdapterView) and as its name says it helps creating multi column list views.

我的应用程序使用MultiColumnListViewhttps://github.com/huewu/PinterestLikeAdapterView),正如其名称所说,它有助于创建多列列表视图。

The library is great, but as they specify, filtering is not supported.

该库很棒,但正如他们指定的那样,不支持过滤。

So I am trying to create my own text-filter functionality with a simple edit text.

所以我试图用一个简单的编辑文本来创建我自己的文本过滤器功能。

Finally I achieve to filter the list but now I need to refresh the listView and recall the setAdapter because I pass the list on it.

最后我实现了过滤列表,但现在我需要刷新 listView 并调用 setAdapter,因为我将列表传递给它。

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

When I execute listView.invalidateViews()or adapter.notifyDataSetChanged()the listView is refreshed but with the old list.

当我执行listView.invalidateViews()adapter.notifyDataSetChanged()listView 刷新但使用旧列表时。

Which is the best way of recalling the setAdapter? or maybe is another way of doing this..

哪个是调用 setAdapter 的最佳方式?或者也许是另一种方式来做到这一点..

Thanks in advance

提前致谢

EDIT:

编辑:

//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());

Log:

日志:

enter image description here

在此处输入图片说明

Adapter Class:

适配器类:

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

}

}

Activity Class:

活动类别:

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

回答by vipul mittal

just change your myListand call adapter.notifyDataSetChanged()don't set a new adapter each time.

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

In the constructor of your custom adapter do call superthat takes ArrayList as the argument.

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

call this:

称之为:

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

You can keep the adapter as it is the problem is in this line:

您可以保留适配器,因为问题出在这一行:

myList = filteredList;

instead of changing the reference you should change the list itself.

您应该更改列表本身,而不是更改引用。

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

By doing this you will loose your original list to I would suggest keeping another list call ed originalList which will have the complete list and initialize myList in onCreate by:

通过这样做,您将丢失原始列表,我建议保留另一个名为 ed originalList 的列表,该列表将包含完整列表并通过以下方式在 onCreate 中初始化 myList:

myList=new ArrayList(originalList);

so every time you want to re-set just call:

所以每次你想重新设置时,只需调用:

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

and in

并在

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

回答by M D

You need to clear older myList.clear()and then call adapter.notifyDataSetChanged()

你需要清除旧的myList.clear()然后打电话adapter.notifyDataSetChanged()

Update:

更新:

Change your code like:

更改您的代码,如:

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

Also, do it like

另外,这样做

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