我花了最后一天尝试使用Google商家信息请求的结果填充我的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>
的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 :(得分: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>