2012-04-15 108 views
2

我花了最後一天試圖用Google Places請求的結果填充我的ListView對象。請求很好,肯定有物品進入我的數據對象。但是當我試圖將這些數據添加到ListView時,無論我嘗試什麼,我總是會得到空行。 getView()被調用,但它似乎沒有效果。下面是代碼中的相關片段:ListView未填充,但getView()被調用

我的main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/gps_activate_button" 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:text="@string/gps_activate_button" 
    android:layout_weight="1" /> 

<ListView 
    android:id="@+id/results_list_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
</ListView> 

<TextView android:id="@id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="No data" /> 

</LinearLayout> 

我的自定義行顯示鄰居信息:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" 
    android:orientation="vertical" > 

    <ImageView android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:contentDescription="@string/custom_row_icon" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="0dip"> 

     <TextView android:id="@+id/name_text_view" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:textColor="#ffffff" /> 
     <TextView android:id="@+id/reference_text_view" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:textColor="#999999" /> 
     <TextView android:id="@+id/rating_text_view" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:textColor="#EBE41C" /> 
    </LinearLayout> 
</LinearLayout> 

的onCreate:

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

    lv = (ListView) findViewById(R.id.results_list_view); 
    gpSearchResponse = new GooglePlacesSearchResponse(); 
    gpAdapter = new GooglePlacesSearchResponseAdapter(this, R.layout.custom_gp_result_row, gpSearchResponse.getGpSearchResults()); 
    lv.setAdapter(gpAdapter); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    viewGooglePlacesSearchResults = new Runnable() { 
     @Override 
     public void run() { 
      Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if (location == null) { 
       location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      } 
      getGooglePlacesSearchResults(location); 
     } 
    }; 
    Thread thread = new Thread(null, viewGooglePlacesSearchResults, "GooglePlacesBackground"); 
    thread.start(); 
    searchResultsDialog = ProgressDialog.show(this, "Please wait...", "Retrieving data ...", true); 
} 

getGooglePlacesSearchResults:

private void getGooglePlacesSearchResults(Location location) { 
    try { 
     GooglePlacesSearchResponseHandler handler = new GooglePlacesSearchResponseHandler(); 
     String FINAL_URL = PLACES_SEARCH_URL + "location=" + location.getLatitude() + "," + location.getLongitude() +"&radius=5000&types=bar%7Cmovie_theater%7Cnight_club%7Crestaurant%7Cshopping_mall&sensor=true&key=AIzaSyD-GWPYtYJMbJuTXuTVXfGXKlVPuWD0d6Q"; 
     JSONObject json = handler.getJSONFromUrl(FINAL_URL); // getting JSON 
     Log.i("URL", FINAL_URL); 
     GooglePlacesSearchResponseParser parser = new GooglePlacesSearchResponseParser(); 
     try { 
      gpSearchResponse = parser.parseResults(json); 
     } catch (JSONException e) { 
      throw new RuntimeException(e); 
     } 
     Thread.sleep(5000); 
     Log.i("ARRAY", ""+ gpSearchResponse.getGpSearchResults().size()); 
    } catch (Exception e) { 
     Log.e("BACKGROUND_PROC", e.getMessage()); 
    } 
    runOnUiThread(returnResults); 
} 

我重寫getView()方法:

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.custom_gp_result_row, null); 
      } 
      GooglePlacesSearchResult gpSearchResult = items.get(position); 
      if (gpSearchResult != null) { 
        //ImageView iconImageView = (ImageView) v.findViewById(R.id.icon); 
        TextView nameTextView = (TextView) v.findViewById(R.id.name_text_view); 
        TextView referenceTextView = (TextView) v.findViewById(R.id.reference_text_view); 
        TextView ratingTextView = (TextView) v.findViewById(R.id.rating_text_view); 
        /*if (iconImageView != null) { 
          iconImageView.setImageURI(Uri.parse(Uri.encode(gpSearchResult.getIconURL()))); 
        }*/ 
        if(nameTextView != null){ 
          nameTextView.setText(gpSearchResult.getName()); 
        } 
        if (referenceTextView != null) { 
          referenceTextView.setText(gpSearchResult.getReference());        
        } 
        if(ratingTextView != null){ 
          ratingTextView.setText(gpSearchResult.getRating()); 
        } 
      } 
      return v; 
    } 

以及通知適配器一個Runnable對象:

private Runnable returnResults = new Runnable() { 
    @Override 
    public void run() { 
     if (gpSearchResponse.getGpSearchResults() != null && gpSearchResponse.getGpSearchResults().size() > 0) { 
      gpAdapter.notifyDataSetChanged(); 
      gpAdapter.setGooglePlacesSearchResult(gpSearchResponse.getGpSearchResults()); 
     } 
     searchResultsDialog.dismiss(); 
     gpAdapter.notifyDataSetChanged(); 
    } 
}; 

我很新的Android開發所以在我的知識差距大,但最好的我可以告訴它看起來getView()是問題的根源,因爲儘管它被調用並確實被我的對象填充,但屏幕上並沒有任何反應。

最後,我根據很多的線程和UI的東西掉這個代碼在這裏:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

感謝您的幫助,您可以提供!

回答

0

我有一位朋友和我一起看代碼,解決方案可悲的是沒有我期望的那麼有趣。事實證明,我的自定義行xml只是一個問題,在那裏我意外地將高度設置爲0.我想如果有什麼這是一個提醒休息一下......下面是更新的XML爲任何人誰感興趣。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" 
    android:orientation="vertical" > 

    <ImageView android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:contentDescription="@string/custom_row_icon" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     android:layout_height="0dip"> 

     <TextView android:id="@+id/name_text_view" 
      android:layout_width="fill_parent" 
      android:layout_height="30sp" 
      android:textColor="#ffffff" /> 
     <TextView android:id="@+id/vicinity_text_view" 
      android:layout_width="fill_parent" 
      android:layout_height="30sp" 
      android:textColor="#999999" /> 
     <TextView android:id="@+id/rating_text_view" 
      android:layout_width="fill_parent" 
      android:layout_height="30sp" 
      android:textColor="#EBE41C" /> 
    </LinearLayout> 
</LinearLayout>