如何使用信息替换ListView或替换

时间:2014-06-11 03:56:59

标签: java android

我正在编写一个复制http://www.rottentomatoes.com/mobile/外观的Android应用程序,我的问题如下 - 我有3个异步任务挖掘json文件并提取每个部分的信息(本周开放,顶部框办公室,也在剧院)然而,当每个列表替换前一个列表,我需要他们追加。我怎么能这样做?

public static class PlaceholderFragment extends Fragment implements OnItemClickListener {

    private InfoAdapter adapter;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        ListView list = (ListView) rootView.findViewById(R.id.list);
        list.setOnItemClickListener(this);
        adapter = new InfoAdapter(inflater);
        list.setAdapter(adapter);
        String link = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?apikey=pgmnsbx5sza3rys7uvtw";
        DownloadMovieInfo task = new DownloadMovieInfo(link, adapter);
        task.execute(new Void[0]);


         link = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/opening.json?apikey=pgmnsbx5sza3rys7uvtw";
        task = new DownloadMovieInfo(link, adapter);
        task.execute(new Void[0]);

        link = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?apikey=pgmnsbx5sza3rys7uvtw";
        DownloadMovieInfo task2 = new DownloadMovieInfo(link, adapter);
        task2.execute(new Void[0]);

        return rootView;
    }

这是异步任务

public class DownloadMovieInfo extends AsyncTask<Void, Void, MovieInfo> {
private String link;
private MovieInfo list;
private InfoAdapter adapter;

public DownloadMovieInfo(String link, InfoAdapter adapter) {
    this.link = link;
    this.adapter = adapter;
}

@Override
protected MovieInfo doInBackground(Void... params) {
    // TODO Auto-generated method stub

    URL url = null;
    URLConnection connection = null;
    Gson gson = new Gson();
    InputStream in = null;
    BufferedReader reader = null;
    try {
        url = new URL(link);
        connection = url.openConnection();
        in = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        list = gson.fromJson(reader, MovieInfo.class);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return list;
}

protected void onPostExecute(MovieInfo result) {
    super.onPostExecute(result);

    adapter.setinfo(result);
    adapter.notifyDataSetChanged();
}

}

我可以想象问题是adapter.setInfo(result);因为这实际上是将数据设置到列表中,但我还没有弄清楚如何附加到列表中。请帮忙

PS从密钥中删除了最后6位数,因此它不适合你

0 个答案:

没有答案