LazyList 的列表视图无法用新的数组列表刷新
LazyList 's listview can not refresh with new arraylist
我正在尝试将 searchView 与 LazyList 结合使用。在我的 lazyAdapter 中,我更新了我的 arraylist,这工作顺利,但我的 listview 没有更新 arrayList。这是我的文件管理器方法。我想 notifyDataSetChanged 没有 work.Please 帮助我如何刷新我的列表视图?
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();
}
LazyList 's listview can not refresh with new arraylist
因为在调用 notifyDataSetChanged()
之前,您没有在 Listview 的当前 Adapter 中添加新列表:
使用 addAll
方法将 list
添加到适配器:
listviewAdapter.addAll(list);
//notify data-source change to adapter
notifyDataSetChanged();
如果 addAll
方法不可用,则在适配器 class 中创建一个方法并将列表作为参数传递,然后调用添加方法:
public void addAll(ArrayList<HashMap<String, String>> data) {
for(String item: data) {
add(item);
}
notifyDataSetChanged();
}
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
变量list
在第三行之后保存了一个不同的对象。您正在更改这个新的 ArrayList
但适配器仍然记得旧的。而不是
list = new ArrayList<HashMap<String, String>>();
写
list.clear();
然后您将使用与之前相同的对象。
我正在尝试将 searchView 与 LazyList 结合使用。在我的 lazyAdapter 中,我更新了我的 arraylist,这工作顺利,但我的 listview 没有更新 arrayList。这是我的文件管理器方法。我想 notifyDataSetChanged 没有 work.Please 帮助我如何刷新我的列表视图?
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();
}
LazyList 's listview can not refresh with new arraylist
因为在调用 notifyDataSetChanged()
之前,您没有在 Listview 的当前 Adapter 中添加新列表:
使用 addAll
方法将 list
添加到适配器:
listviewAdapter.addAll(list);
//notify data-source change to adapter
notifyDataSetChanged();
如果 addAll
方法不可用,则在适配器 class 中创建一个方法并将列表作为参数传递,然后调用添加方法:
public void addAll(ArrayList<HashMap<String, String>> data) {
for(String item: data) {
add(item);
}
notifyDataSetChanged();
}
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
变量list
在第三行之后保存了一个不同的对象。您正在更改这个新的 ArrayList
但适配器仍然记得旧的。而不是
list = new ArrayList<HashMap<String, String>>();
写
list.clear();
然后您将使用与之前相同的对象。