LazyList的listview无法使用新的arraylist刷新

时间:2015-01-17 11:40:39

标签: java android listview searchview lazylist

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

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

3 个答案:

答案 0 :(得分:1)

  

LazyList的listview无法使用新的arraylist刷新

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

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

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

答案 1 :(得分:1)

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

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

list.clear();

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

答案 2 :(得分:0)

        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