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

时间:2016-02-15 18:46:46

标签: java android android-asynctask

我正在设计一个用于展示不同电影的电影应用,

我调用AsyncTask线程并接收返回的调用:

@Override
public void processFinish(final ArrayList<Film> output) {
    generateFilmList(output);
}

使用电影数据我生成列表视图:

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

}

这是我的列表适配器:

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

}

我在每个页面的列表视图中只显示20部电影,因此在调用新页面时,我会生成一个包含新页面值的新查询:

public void nextPage(View view) {
    this.page = page + 1;
    new Debugger().print(Integer.toString(page));
    generateFilmQuery();
}

这也会使用新值更新我的列表视图。

我的问题是虽然列表视图已更新,因为列表适配器已被更改。在我向下滚动页面之前,屏幕不显示更改。这是没有意义的,因为我打电话给notifyDataSetChanged,虽然这没有做任何事情!

1 个答案:

答案 0 :(得分:0)

我希望它对你有所帮助。

在ListAdapter中添加方法

vim /etc/xinetd.d/nrpe
only_from= 127.0.0.1 172.16.16.150

并像这样更改您的代码

pubilc void addItems(ArrayList<Film> films)
{
   this.films = films;
}