notifyDataSetChanged不工作工作、notifyDataSetChanged

2023-09-06 18:16:47 作者:无语回眸笑红尘

我的应用程序使用了 MultiColumnListView (的https:// github上.COM / huewu / PinterestLikeAdapterView ),并为它的名字说,这有助于创建多列列表视图。

图书馆是伟大的,但是,当他们指定,不支持过滤。

所以,我想用一个简单的编辑文本创建自己的文字过滤功能。

我终于实现过滤列表,但现在我需要刷新ListView和召回setAdapter,因为我通过列表中就可以了。

  MyAdapter适配器=新MyAdapter(这一点,myList中);
listView.setAdapter(适配器);
 

当我执行 listView.invalidateViews() adapter.notifyDataSetChanged()在ListView被刷新,但与老列表。

这是回顾setAdapter的最佳方法是什么?或者是这样做的另一种方式。

在此先感谢

编辑:

  //方法筛选器列表
Log.i(开滤波器myList.size(),+ myList.size());
adapter.notifyDataSetChanged();

//在适配器
Log.i(关于适配器myList.size(),+ myList.size());
 

登录

适配器

 公共类MiAdaptadorListaComercios扩展了BaseAdapter {

// TextView的和Imageviews声明

私人语境contexto;

私人列表<商报> myList中;

公共MiAdaptadorListaComercios(上下文C,名单,其中,商报> myList中){
    this.contexto = C;
    this.myList =新的ArrayList<商报>();
    this.myList = myList中;
}
@覆盖
公众诠释getCount将(){
    Log.i(关于适配器myList.size(),+ listaComercios.size());
    返回myList.size();
}

@覆盖
公共对象的getItem(INT位置){
    返回myList.get(位置);
}

@覆盖
众长getItemId(INT为arg0){
    // TODO自动生成方法存根
    返回0;
}

@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
    查看查看= NULL;

    如果(convertView == NULL)
    {
        LayoutInflater充气=(LayoutInflater)contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        鉴于= inflater.inflate(R.layout.list_cell,NULL);
    }
    其他{
        鉴于= convertView;
    }

    //设置意见Texteviews文字,字体等...

    返回查看;
}
 
尖锐湿疣间接传染的几率大吗

}

活动

公共类ListaComercio延伸活动{

 私人MultiColumnListView mAdapterView = NULL;
EditText上搜索栏;
私人的ArrayList<商报> myList中;
私人的ArrayList<商报> filteredList;
MyAdapter适配器;


公共ListaComercio(){
    myList上=新的ArrayList<商报>();
    filteredList =新的ArrayList<商报>();
}

@覆盖
保护无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.lista_comercios);

    mAdapterView =(MultiColumnListView)findViewById(R.id.list);

    适配器=新MyAdapter(ListaComercio.this,myList中);

    mAdapterView.setAdapter(适配器);


    sea​​rchBar.setOnEditorActionListener(新TextView.OnEditorActionListener(){
        @覆盖
        公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
            如果(actionId == EditorInfo.IME_ACTION_SEARCH){
                CharSequence的滤波器= v.getText();

                对于(商报联合:myList上)
                {
                    如果(co.getName()。包含(文件服务器))
                    {
                        filteredList.add(共);
                    }
                }

                myList中= filteredList;

                Log.i(开滤波器myList.size(),+ myList.size());

                myList.clear();
                adapter.notifyDataSetChanged();
                //mAdapterView.invalidateViews();

                返回true;
            }
            返回false;
        }
    });
}
}
 

解决方案

只是改变你的 myList上和呼叫 adapter.notifyDataSetChanged()不要每次都设置一个新的适配器。

在自定义适配器的构造做电这需要的ArrayList作为参数。

称之为:

 公共MiAdaptadorListaComercios(上下文C,名单,其中,商报> myList中){
    超级(C,0,myList中);
    this.contexto = C;
    this.myList = myList中;
}
 

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

  myList中= filteredList;
 

而不是改变基准你应该改变列表本身。

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

这样做,你将失去你原来的清单,我会建议保持另一个列表调用编辑originalList这将有完整的列表,并在初始化的onCreate通过myList中:

  myList上=新的ArrayList(originalList);
 

所以每次要重新设置只要致电:

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

 的(商报联合:originalList)
            {
                如果(co.getName()。包含(文件服务器))
                {
                    filteredList.add(共);
                }
            }
 

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

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.

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.

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

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

Adapter

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

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

解决方案

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

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

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:

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