【问题标题】:LazyList 's listview can not refresh with new arraylistLazyList 的列表视图不能用新的 arraylist 刷新
【发布时间】:2015-03-15 22:47:50
【问题描述】:

我正在尝试将 searchView 与 LazyList 一起使用。在我的lazyAdapter 中,我正在更新我的arraylist,这很顺利,但是我的listview 没有用arrayList 更新。这是我的文件管理器方法。我想 notifyDataSetChanged 不起作用。请帮助我如何刷新我的列表视图?

public void filter(String charText) {

    charText = charText.toLowerCase(Locale.getDefault());
    //list.clear();
    list = new ArrayList<HashMap<String, String>>();
    if (charText.length() == 0) {
        list.addAll(arraylist);
    } 
    else 
    {
        for (HashMap<String, String> map : arraylist) 
        {
            if (map.get("radio").toString().toLowerCase(Locale.getDefault()).contains(charText)) 
            {
                list.add(map);
            }
        }
    }
    notifyDataSetChanged();
}

【问题讨论】:

  • 重新设置列表视图的适配器,例如 lv.setAdapter(adapter);

标签: java android listview searchview lazylist


【解决方案1】:

LazyList 的 listview 不能用新的 arraylist 刷新

因为在调用notifyDataSetChanged() 之前,您没有在 Listview 的当前适配器中添加新列表:

使用addAll 方法将list 添加到适配器:

listviewAdapter.addAll(list);
//notify data-source change to adapter
 notifyDataSetChanged();

如果 addAll 方法不可用,则在 Adapter 类中创建一个方法并将列表作为参数传递,然后调用 add 方法:

public void addAll(ArrayList<HashMap<String, String>> data) {
     for(String item: data) {
         add(item);
       }
       notifyDataSetChanged();
  }

【讨论】:

    【解决方案2】:
            public class MovieListAdapter extends BaseAdapter {
            private Context context;
            ArrayList<HashMap<String, String>> movielist;
            private ArrayList<HashMap<String, String>> listAdapter;
    
            public MovieListAdapter(Context context, ArrayList<HashMap<String, String>> movielist) {
                this.context = context;
                this.movielist = movielist;
                this.listAdapter = new ArrayList<HashMap<String, String>>();
                this.listAdapter.addAll(movielist);
    
            }
    
            @Override
            public int getCount() {
    
                return movielist.size();
            }
    
            @Override
            public Object getItem(int position) {
                return movielist.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                View griView = inflater.inflate(R.layout.movie_listitem, null);
                TextView textview = (TextView) griView.findViewById(R.id.catname);
    
                textview.setText(movielist.get(position).get("name"));
                textview.setSelected(true);
                ImageView imageView = (ImageView) griView.findViewById(R.id.catimage);
    
                DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                int width = metrics.widthPixels / 3;
                int height = metrics.widthPixels / 3;
    
                // System.out.println(movielist.get(position).getMoviepicture());
    
                if (movielist.get(position).get("image").equals("")) {
                    imageView.setImageResource(R.drawable.blankart);
                } else {
                    Picasso.with(context)
                            .load(movielist.get(position).get("image"))
                            .resize(width, height).placeholder(R.drawable.blankart)
                            .noFade().into(imageView);
                }
                return griView;
            }
    
            public void filter(String charText) {
                charText = charText.toLowerCase(Locale.getDefault());
                movielist.clear();
                if (charText.length() == 0) {
                    movielist.addAll(listAdapter);
                } else {
                    for (HashMap<String, String> si : listAdapter) {
                        if (si.get("name").toLowerCase(Locale.getDefault())
                                .contains(charText)) {
                            movielist.add(si);
                        }
                    }
                }
                notifyDataSetChanged();
            }
        }
        make you adapter like this
    

    【讨论】:

      【解决方案3】:

      变量list 在第三行之后保存了一个不同的对象。您正在更改这个新的ArrayList,但适配器仍会记住旧的。而不是

      list = new ArrayList<HashMap<String, String>>();
      

      写

      list.clear();
      

      然后您将使用与以前相同的对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2011-03-18
        • 2011-05-04
        • 1970-01-01
        相关资源
        最近更新 更多