Diana
Diana

Reputation: 1437

Android delete all items in gridview (refresh gridview)

I'm trying to make an app that gets different photos from an online server depending on the marker the user clicked. My problem is that for example, if the user clicked marker1 they'll get the pictures for marker1 but when they try to see the photos for marker2, my gridview continues to show the photos for marker1 (without any changes - deleting/adding new photos). Same if the user starts with marker2.

Therefore I don't understand what I'm doing wrong as I set the adapter to null gridView1.setAdapter(null); and then I search for the images for the clicked marker and then display them in the gridview. But my pictures from the gridview won't disappear even though I set the adapter to null.

Here's my main code:

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

Any idea what might be wrong or if there is any better way?

Upvotes: 0

Views: 689

Answers (2)

Try to set your image_urls = null when you had clicked.

Upvotes: 1

Pavya
Pavya

Reputation: 6025

Its because for the first time what you get on click of marker1 is get added in your image_urls. When you click on marker2 then at that time all data is available. For that you need to set image_urls = new ArrayList<>(); in each click.

Upvotes: 1

Related Questions