2

昨日、ListViewオブジェクトにGoogleプレイスリクエストの結果を入力しようとして過ごしました。リクエストは問題ありません。私のデータオブジェクトには間違いなくアイテムが入っています。しかし、このデータを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()が呼び出されて実際にいっぱいになるにもかかわらず、getView()が問題の原因であるように思われます。私のオブジェクト、画面には何も起こりません。

最後に、私はここでこのコードに基づいてスレッドとUIの多くをベースにしました:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

あなたが提供できるどんな助けにも感謝します!

4

1 に答える 1

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>
于 2012-04-16T15:02:05.897 に答える