使ArrayList仅显示新数据

问题描述 投票:0回答:3

我有一个应用程序可以显示来自tumblr帐户的图像。在列表的底部,有一个“加载更多”按钮,按下该按钮可将20张图像加载到列表中。由于现在可以正常使用,该应用程序仅将新图像追加到列表中,因此在按下按钮后将显示40张图像。我如何才能使列表仅包含新的20张图像?

我的代码如下:

public class Example extends Activity {

static ArrayList<Tumblr> tumblrs;
Context context = null;
ListView listView = null;
TextView footer;
int offset = 0;
ProgressDialog pDialog;
public boolean isNew = false;

private String searchUrl = "http://api.tumblr.com/v2/blog/factsandchicks.com/posts?api_key=API_KEY&offset=0";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        tumblrs = getTumblrs();
        listView = (ListView) findViewById(R.id.list);
        View v = getLayoutInflater().inflate(R.layout.footer_layout, null);
        footer = (TextView) v.findViewById(R.id.tvFoot);
        listView.addFooterView(v);
        listView.setAdapter(new UserItemAdapter(this, R.layout.listitem));

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    footer.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new loadMoreListView().execute();
        }
    });

}

public class UserItemAdapter extends ArrayAdapter<Tumblr> {

    public UserItemAdapter(Context context, int imageViewResourceId) {
        super(context, imageViewResourceId, tumblrs);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listitem, null);
        }

        Tumblr tumblr = tumblrs.get(position);
        if (tumblr != null) {
            ImageView image = (ImageView) v.findViewById(R.id.avatar);
            if (image != null) {
                image.setImageBitmap(getBitmap(tumblr.image_url));
            }
        }
        return v;
    }
}

public Bitmap getBitmap(String bitmapUrl) {
    try {
        URL url = new URL(bitmapUrl);
        return BitmapFactory.decodeStream(url.openConnection()
                .getInputStream());
    } catch (Exception ex) {
        return null;
    }
}

public ArrayList<Tumblr> getTumblrs() throws ClientProtocolException,
        IOException, JSONException {
    searchUrl = "http://api.tumblr.com/v2/blog/factsandchicks.com/posts?api_key=API_KEY&offset=0";

    ArrayList<Tumblr> tumblrs = new ArrayList<Tumblr>();

    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(searchUrl);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    String responseBody = null;
    try {
        responseBody = client.execute(get, responseHandler);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    JSONObject jsonObject = new JSONObject(responseBody);

    JSONArray posts = jsonObject.getJSONObject("response").getJSONArray(
            "posts");
    for (int i = 0; i < posts.length(); i++) {
        JSONArray photos = posts.getJSONObject(i).getJSONArray("photos");
        for (int j = 0; j < photos.length(); j++) {
            JSONObject photo = photos.getJSONObject(j);
            String url = photo.getJSONArray("alt_sizes").getJSONObject(0)
                    .getString("url");

            Tumblr tumblr = new Tumblr(url);
            tumblrs.add(tumblr);
        }
    }
    return tumblrs;
}

public class Tumblr {

    public String image_url;

    public Tumblr(String url) {
        this.image_url = url;
    }
}

private class loadMoreListView extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(Example.this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... unused) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            public void run() {
                // increment current page
                offset += 2;

                // Next page request

                searchUrl = "http://api.tumblr.com/v2/blog/factsandchicks.com/posts?api_key=API_KEY&offset="
                        + offset;

                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(searchUrl);

                ResponseHandler<String> responseHandler = new BasicResponseHandler();

                String responseBody = null;
                try {
                    responseBody = client.execute(get, responseHandler);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                JSONObject jsonObject;
                try {
                    jsonObject = new JSONObject(responseBody);

                    JSONArray posts = jsonObject.getJSONObject("response")
                            .getJSONArray("posts");
                    for (int i = 0; i < posts.length(); i++) {
                        JSONArray photos = posts.getJSONObject(i)
                                .getJSONArray("photos");
                        for (int j = 0; j < photos.length(); j++) {
                            JSONObject photo = photos.getJSONObject(j);
                            String url = photo.getJSONArray("alt_sizes")
                                    .getJSONObject(0).getString("url");

                            Tumblr tumblr = new Tumblr(url);
                            tumblrs.add(tumblr);

                        }
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // get listview current position - used to maintain scroll
                // position
                int currentPosition = listView.getFirstVisiblePosition();

                // Setting new scroll position
                listView.setSelectionFromTop(currentPosition + 1, 0);
            }
        });
        return null;
    }

    protected void onPostExecute(Void unused) {
        pDialog.dismiss();
    }

}
}

需要任何帮助。

java android arraylist load
3个回答
0
投票

只需像这样重新初始化列表(这会在loadMoreListView#doInBackground方法中添加一行)。

tumblrs = new ArrayList<Tumblr> ();
for (int i = 0; i < posts.length(); i++) {
    JSONArray photos = posts.getJSONObject(i).getJSONArray("photos");
    for (int j = 0; j < photos.length(); j++) {
         JSONObject photo = photos.getJSONObject(j);
         String url =
             photo.getJSONArray("alt_sizes").getJSONObject(0).getString("url");
         Tumblr tumblr = new Tumblr(url);
         tumblrs.add(tumblr);
         }
}

PS:您添加了太多不需要的代码。


0
投票

使用

tumblrs.clear();

在您的doInBackground()方法中


0
投票

非常简单,我通常在项目中使用它。

只需在clear()上调用ArrayList方法,该方法就会进入ArrayAdpater

clear()上调用ArrayList 之前加载新数据,将清空以前的数据,并用新的20个条目填充它。

© www.soinside.com 2019 - 2024. All rights reserved.