简体   繁体   English

添加新项目时,ListView在屏幕上未更新

[英]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.

相关问题 不要为添加到列表视图中的新项目设置标签 - Do not settag for new items that is added into listview 添加新记录时,ListView不会刷新 - ListView Not Refreshing When New Record Added 添加新标签时更新JTabbedPane - Updating JTabbedPane when new tab is added 添加到observableList时呈现的项目的JavaFX listview复制 - JavaFX listview duplication of rendered items when added to observableList RecyclerView 仅在添加更多项目时显示新项目 - RecyclerView only shows new items when more items are added 添加启动画面时,webview URL 未通过 FCM 更新 - webview URL not updating via FCM when Splash Screen is added 当自定义列表视图项不在屏幕上时,如何保持它们? - How to keep custom listview items when they are out of screen? 切换内容视图时,Android ListView不显示新添加的项目并清除旧项目 - Android ListView not showing newly added items and erasing old items when switching content views ListView在较小的屏幕上不显示项目 - ListView not showing items on a smaller screen 当我手动更改 firebase 数据库中的值时,我在 listview 中创建了更多项目,而不是在 listview 中更新值 - when I manually change value in firebase database I create more items in listview instead of updating value in listview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM