[英]ListView not updating on screen when new items are added
I am designing a film application for displaying different films, 我正在设计一个电影应用程序来显示不同的电影,
I make a call to an AsyncTask thread and receive the returning call : 我对AsyncTask线程进行了调用,并收到返回的调用:
@Override
public void processFinish(final ArrayList<Film> output) {
generateFilmList(output);
}
Using the film data i generate the list view : 使用电影数据,我生成列表视图:
public void generateFilmList(final ArrayList<Film> films) {
ListView lv = (ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Intent intent = new Intent(Film_List.this, Film_Details.class);
Bundle b = new Bundle();
b.putParcelable("film", films.get(position));
intent.putExtras(b);
startActivity(intent);
}
});
if (la == null) {
la = new ListAdapter(this, films);
lv.setAdapter(la);
} else {
runOnUiThread(new Thread(new Runnable() {
public void run() {
la.clear();
la.addAll(films);
}
}));
}
la.notifyDataSetChanged();
lv.setSelectionAfterHeaderView();
}
This is my list adapter : 这是我的列表适配器:
public class ListAdapter extends ArrayAdapter<Film> {
private final Context context;
private ArrayList<Film> films;
private View rowView;
public ListAdapter(Context context, ArrayList<Film> films) {
super(context, R.layout.row, films);
this.context = context;
this.films = films;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
generateTitle(position);
generateTag(position);
generateImage(position);
return rowView;
}
public void generateTitle(final int position) {
TextView title = (TextView) rowView.findViewById(R.id.text_title);
title.setText(films.get(position).getTitle());
}
public void generateTag(final int position) {
TextView tag = (TextView) rowView.findViewById(R.id.text_description);
tag.setText(films.get(position).getTag());
}
public void generateImage(final int position) {
final ImageView image = (ImageView) rowView.findViewById(R.id.image_poster);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(films.get(position).getImg());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
image.setImageBitmap(bmp);
} catch (Exception e) {
new Debugger().print(e.toString());
}
}
});
thread.start();
}
}
I display only 20 films in a list view for each page, so when calling a new page i generate a new query with the new page value: 我在每个页面的列表视图中仅显示20部电影,因此在调用新页面时,我会使用新页面值生成一个新查询:
public void nextPage(View view) {
this.page = page + 1;
new Debugger().print(Integer.toString(page));
generateFilmQuery();
}
This also updates my listview with the new values. 这也会用新值更新我的列表视图。
My problem is that although the list view is updated because the list adapter has been altered. 我的问题是,尽管列表视图已更新,因为列表适配器已更改。 The screen is not displaying the changes until I SCROLL down on the page. 直到我向下滚动页面,屏幕才显示更改。 This doesn't make sense as I am calling notifyDataSetChanged
, although this doesn't do anything! 这没有意义,因为我正在调用notifyDataSetChanged
,尽管这没有任何作用!
I hope it will helpful for you. 希望对您有帮助。
Add a method in ListAdapter 在ListAdapter中添加方法
pubilc void addItems(ArrayList<Film> films)
{
this.films = films;
}
And change your code like this 然后像这样更改代码
runOnUiThread(new Thread(new Runnable() {
public void run() {
la.addItems(new ArrayList<Film>());
la.notifyDataSetChanged();
la.addItems(films);
la.notifyDataSetChanged();
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.