2015-05-06 17 views
0

我想通過使用從Parse查詢中獲得的信息在我的應用程序中創建自定義infowindow。我遇到的問題是,我只能從主線程更新UI,而不是Parse查詢使用的後臺進程。從Parse查詢更新InfoWindow Google地圖Android

map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
      @Override 
      public View getInfoWindow(Marker marker) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 
       View v = getLayoutInflater().inflate(R.layout.info_window, null); 
       TextView titleInfoWindow = (TextView) v.findViewById(R.id.titleInfoWindow); 
       final TextView dateInfoWindow = (TextView) v.findViewById(R.id.dateInfoWindow); 
       TextView timeInfoWindow = (TextView) v.findViewById(R.id.timeInfoWindow); 
       TextView latitudeInfoWindow = (TextView) v.findViewById(R.id.latitudeInfoWindow); 
       TextView sexInfoWindow = (TextView) v.findViewById(R.id.sexInfoWindow); 
       TextView speciesInfoWindow = (TextView) v.findViewById(R.id.speciesInfoWindow); 

       titleInfoWindow.setText(marker.getTitle()); 
       latitudeInfoWindow.setText(String.valueOf(marker.getPosition().latitude)); 


       ParseQuery<ParseObject> query = ParseQuery.getQuery("deletingTest"); 
       query.whereEqualTo("shark", marker.getTitle()); 
       query.whereEqualTo("latitude", String.valueOf(marker.getPosition().latitude)); 
       query.whereEqualTo("longitude", String.valueOf(marker.getPosition().longitude)); 
       query.setLimit(100); 
       query.findInBackground(new FindCallback<ParseObject>() { 
        public void done(List<ParseObject> scoreList, ParseException e) { 
         if (scoreList.size() == 0) { 
          Log.d("MainActivity", "0"); 
         } else { 
          Log.d("MainActivity", String.valueOf(scoreList.size())); 
          dateInfoWindow.setText("22"); 
         } 
        } 
       }); 


      return v; 
     } 
    }); 

因此,雖然dateInfoWindow是不是因爲它不是在主線程的titleInfoWindow和latitudeInfoWindow正在更新。能從數據中更新信息窗口的最佳方式是什麼?

這是我用於infoWindow佈局的XML文件。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/titleInfoWindow" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/dateInfoWindow"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/timeInfoWindow"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/latitudeInfoWindow"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/longitudeInfoWindow"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/sexInfoWindow"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" 
      android:textColor="#000000" 
      android:id="@+id/speciesInfoWindow"/> 
</LinearLayout> 
+1

請嘗試在'done'方法中更新'infoWindow'信息 – bjiang

+0

@bjiang我試圖在else子句中使用dateInfoWindow.setText(「22」);然而,這對infoWindow沒有任何作用,因爲至少我相信,這不在主線程中,因此不會更新ui。 –

+0

您可以檢查您是否嘗試使用日誌輸出在主線程或後臺線程中設置文本 - 只需將線程'Thread.currentThread()。getName()'傳遞給日誌即可。 –

回答

0

雖然Parse查詢在後臺線程中運行,但此查詢的回調將在主Ui線程中執行。所以它不應該是原因。

您無法正確設置數據的原因是您用錯誤的方法膨脹了自定義佈局。 According to Google

替換默認的信息窗口,與您的自定義繪製覆蓋getInfoWindow(標記)和getInfoContents(標記)返回null。

由於您試圖不替換默認InfoWindow的內容,但要膨脹自己的自定義佈局,您必須將其膨脹到另一個方法。

+0

如果我把它放到'getInfoMethod'並返回視圖,它根本不顯示任何東西;然而,我在'getInfoContents'中得到的東西確實更新了'infoWindow',除了解析查詢的'done'方法中的一行之外 –

+0

根據Google的說法,'infoWindow'是一個靜態圖像,不能更改一次它已被設置。所以我相信我的'View v'在解析查詢完成之前被返回,並且done方法會運行,因爲如果我將其更改爲查詢外的文本視圖,它將起作用。有沒有辦法只在'done'完成後返回視圖? –

+0

所以解決方案是顯而易見的 - 然後預取您的數據,然後設置適配器 –