【发布时间】:2015-01-20 06:13:13
【问题描述】:
我在 listView 中没有看到任何数据,我从 Web 服务获取所有数据,但在 ListView 中没有看到它。 我得到了流,我明白它永远不会进入 getView() 方法 请向我解释该代码中出了什么问题并建议我最好的。
public class WeatherReport extends Activity {
ListView listView;
GPS_Location gpsObj;
BaseAdapter aAdapter;
double Latitude, Longitude;
Bitmap image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather_report);
listView = (ListView) findViewById(R.id.listView);
gpsObj = new GPS_Location(this);
Latitude = gpsObj.getLatitude();
Longitude = gpsObj.getLongitude();
System.out.println("Inside onCreate() ");
String Url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=" + Latitude + "&lon=" + Longitude + "&cnt=14&mode=json";
new getJSON().execute(Url);
// listView.setAdapter(new BAdapter(this));
}
private class getJSON extends AsyncTask<String, String, String> {
String cityName;
String Latitude, Longitude;
ArrayList<SingleRow> list;
ArrayList<String> humidity;
ArrayList<String> speed;
ArrayList<String> weather;
ArrayList<String> tempMin;
ArrayList<String> tempMax;
ArrayList<String> description;
ArrayList<Bitmap> icon;
@Override
protected String doInBackground(String... params) {
String Url = params[0];
humidity = new ArrayList<String>();
speed = new ArrayList<String>();
weather = new ArrayList<String>();
tempMin = new ArrayList<String>();
tempMax = new ArrayList<String>();
icon = new ArrayList<Bitmap>();
description = new ArrayList<String>();
String data;
try {
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet(Url);
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
System.out.println("Inside doInBackground data " + data);
JSONObject jObj = new JSONObject(data);
System.out.println("Inside Background jObj " + jObj);
JSONObject jsonObject = jObj.getJSONObject("city");
cityName = jsonObject.getString("name");
JSONObject jObjCoOrd = jsonObject.getJSONObject("coord");
Latitude = jObjCoOrd.getString("lat");
System.out.println("Inside Latitude " + Latitude);
Longitude = jObjCoOrd.getString("lon");
System.out.println("Inside Longitude " + Longitude);
JSONArray jsonArray = jObj.getJSONArray("list");
System.out.println("Inside jsonObjList " + jsonArray);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String humidityString = object.getString("humidity");
humidity.add(humidityString);
String speedString = object.getString("speed");
System.out.println("Inside speedString " + speedString);
speed.add(speedString);
JSONArray weatherArray = object.getJSONArray("weather");
JSONObject weatherObj = weatherArray.getJSONObject(0);
String iconString = weatherObj.getString("icon");
final String iconURL = "http://openweathermap.org/img/w/" + iconString + ".png";
image = getImage(iconURL);
icon.add(image);
System.out.println("Inside icon " + icon);
String descriptionString = weatherObj.getString("description");
System.out.println("Inside descriptionString " + descriptionString);
description.add(descriptionString);
JSONObject tempObj = object.getJSONObject("temp");
String minTemp = tempObj.getString("min");
System.out.println("Inside minTemp " + minTemp);
tempMin.add(minTemp);
String maxTemp = tempObj.getString("max");
System.out.println("Inside maxTemp " + maxTemp);
tempMax.add(maxTemp);
}
aAdapter = new ListViewCustomAdapter(WeatherReport.this,
Latitude, Longitude, cityName, humidity, speed, icon, description, tempMin, tempMax);
listView.setAdapter(aAdapter);
} catch (Exception e) {
Log.w("Error", e.getMessage());
}
return null;
}
private Bitmap getImage(String iconURL) {
InputStream in = null;
Bitmap bmp = null;
int responseCode = -1;
try {
URL url = new URL(iconURL);//"http://192.xx.xx.xx/mypath/img1.jpg
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.connect();
responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//download
in = con.getInputStream();
bmp = BitmapFactory.decodeStream(in);
in.close();
}
} catch (Exception ex) {
Log.e("Exception", ex.toString());
}
return bmp;
}
private class ListViewCustomAdapter extends BaseAdapter {
Context context;
public LayoutInflater inflater;
String Latitude, Longitude, cityName;
String[] Humidity, Speed, Description1, minTemp, maxTemp ;
ArrayList<Bitmap> bitmap;
public ListViewCustomAdapter(Context context, String latitude, String longitude, String cityName, ArrayList<String> humidity, ArrayList<String> speed, ArrayList<Bitmap> image, ArrayList<String> description, ArrayList<String> tempMin, ArrayList<String> tempMax) {
this.context = context;
this.Latitude = latitude;
this.Longitude = longitude;
this.cityName = cityName;
this.bitmap = icon;
this.Humidity = humidity.toArray(new String[humidity.size()]);
this.Speed = speed.toArray(new String[speed.size()]);
this.Description1 = description.toArray(new String[description.size()]);
this.minTemp = tempMin.toArray(new String[tempMin.size()]);
this.maxTemp = tempMax.toArray(new String[tempMax.size()]);
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.out.println("Inside ListViewCustomAdapter ");
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
private class Holder {
TextView City, MinTemp, MaxTemp, Description, Speed, Latitude, Longitude, Humidity;
ImageView cloud;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder= null;
View view = convertView;
System.out.println("Inside getView");
if(view== null){
System.out.println("Inside if getView");
holder = new Holder();
convertView = inflater.inflate(R.layout.weather_report_single_item, null);
holder.cloud = (ImageView) findViewById(R.id.cloud);
holder.City = (TextView) findViewById(R.id.cityName);
holder.MinTemp= (TextView) findViewById(R.id.minTemp);
holder.MaxTemp= (TextView) findViewById(R.id.maxTemp);
holder.Description=(TextView) findViewById(R.id.weather);
holder.Speed =(TextView) findViewById(R.id.speed);
holder.Latitude = (TextView) findViewById(R.id.latitude);
holder.Longitude= (TextView) findViewById(R.id.longitude);
holder.Humidity= (TextView) findViewById(R.id.humidity);
convertView.setTag(holder);
}else{
System.out.println("Inside else getView");
holder = (Holder) convertView.getTag();
holder.City.setText(cityName);
holder.Latitude.setText(Latitude);
holder.Longitude.setText(Longitude);
holder.MaxTemp.setText(maxTemp[position]);
holder.MinTemp.setText(minTemp[position]);
holder.Speed.setText(Speed[position]);
holder.Description.setText(Description1[position]);
holder.cloud.setImageBitmap(bitmap.get(position));
}
return convertView;
}
}
}
}
class SingleRow {
String minTemp, maxTemp, speed, humidity;
Bitmap img;
SingleRow(String minTemp, String maxTemp, String speed, String humidity, Bitmap img) {
this.maxTemp = maxTemp;
this.minTemp = minTemp;
this.speed = speed;
this.humidity = humidity;
this.img = img;
}
}
【问题讨论】:
-
您在 getview 方法的 else 部分设置数据。在 else 之后设置它们。
-
@DhavalGondaliya 我这样做了,但仍然没有得到任何东西......
-
inside minTemp 286.93 inside maxTemp 300.91 Inside ListViewCustomAdapter 这些是我在 logcat 中得到的流程,你可以看到我从不进入 getView()
-
您没有在活动中使用自定义适配器。使用自定义适配器而不是 baseadapter
标签: android listview android-arrayadapter