Android删除gridview中的所有项目(刷新gridview)

时间:2015-07-28 11:42:50

标签: android gridview

我尝试根据用户点击的标记创建一个从在线服务器获取不同照片的应用。我的问题是,例如,如果用户点击marker1他们会获得marker1的图片,但当他们尝试查看marker2的照片时,我的网格视图会继续显示marker1的照片(没有任何更改 - 删除/添加新照片)。如果用户以marker2开头,则相同。

因此,当我将适配器设置为null gridView1.setAdapter(null);然后我搜索所点击标记的图像然后在gridview中显示它们时,我不明白我做错了什么。但是,即使我将适配器设置为null,我的gridview图片也不会消失。

这是我的主要代码:

  public static ArrayList<String> image_urls = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_poi_photos);

            // Check for double values - clear the gridview. reload the images
            //image_urls.clear();
            gridView1 = (GridView) findViewById(R.id.gridview);
            if (gridView1.getChildCount() != 0 || image_urls != null) {
                gridView1.setAdapter(null);
            }
            if (image_urls.isEmpty())
            {
                getImages();
            }

            final ImageAdapter adapter = new ImageAdapter(this);
            adapter.notifyDataSetChanged();
            gridView1.setAdapter(adapter);
    }


    // GETTING PHOTOS FROM DATABASE
    protected void showList() {
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            photos = jsonObj.getJSONArray(TAG_RESULTS);

            for (int i = 0; i < photos.length(); i++) {
                JSONObject c = photos.getJSONObject(i);
                String email_user = c.getString(TAG_EMAIL_USER);
                String poi_name = c.getString(TAG_POI_NAME);
                String photo_url = c.getString(TAG_PHOTO_URL);

                if ((poi_name.substring(0, 4)).equals(map.markername) && photo_url!=null) {
                   // add to the picasso gallery
                    photo_url="http:/xxxxx/photos/"+photo_url;
                   image_urls.add(photo_url);
                }

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


    public void getImages() {
        class GetDataJSON extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://xxxxx/table_photo_out.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                } finally {
                    try {
                        if (inputStream != null) inputStream.close();
                    } catch (Exception squish) {
                    }
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                myJSON = result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }

知道可能出现的问题或是否有更好的方法?

2 个答案:

答案 0 :(得分:1)

这是因为你第一次点击marker1会在你的image_urls中添加。当您单击marker2时,那时所有数据都可用。 为此你需要设置image_urls = new ArrayList&lt;&gt;();在每次点击中。

答案 1 :(得分:1)

点击后尝试设置image_urls = null。