2015-07-28 72 views
0

我试图让一个应用程序从一个在线服务器获取不同的照片,取决于用户点击的标记。我的问题是,例如,如果用户点击marker1,他们会得到marker1的图片,但当他们尝试查看​​的照片时,我的gridview继续显示marker1的照片(没有任何更改 - 删除/添加新照片)。如果用户以​​开头,则相同。Android删除gridview中的所有项目(刷新gridview)

因此,我不明白我在做什么错误,因为我将适配器设置为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(); 
    } 

任何想法可能是错误的,或者有什么更好的办法?

+0

你在getImages中设置适配器吗? – Pavya

+0

@ user3676184不,我只是在getImages()调用 – Diana

+0

后,在onCreate中设置我的适配器,您将获得列表或数组中的所有图像。意味着如何迭代所有图像? – Pavya

回答

1

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

1

尝试设置你的image_urls = null当你点击。