2012-04-15 14 views
2

Googleプレイスのリクエストの結果を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> 
関連する問題