2

我花了最后一天尝试用 Google Places 请求的结果填充我的 ListView 对象。请求很好,肯定有项目进入我的数据对象。但是当我尝试将此数据添加到 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>

创建:

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);
}

获取 GooglePlaces 搜索结果:

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/

感谢您的任何帮助,您可以提供!

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 回答