Android 解析JSON数据并存储到listview中
我目前正在开发一个小型android天气应用程序。它通过3个字符串来显示来自开源weatherAPI的三个天气元素。我正试图在Android 解析JSON数据并存储到listview中,android,json,string,listview,android-arrayadapter,Android,Json,String,Listview,Android Arrayadapter,我目前正在开发一个小型android天气应用程序。它通过3个字符串来显示来自开源weatherAPI的三个天气元素。我正试图在列表视图中显示此内容。我在传入一个字符串时使其工作。但我在传递多个字符串时遇到问题。如有任何建议,将不胜感激 ArrayList<Weather> weatherData = new ArrayList<Weather>(); private ListView listView1; @Override protected
列表视图中显示此内容。我在传入一个字符串时使其工作。但我在传递多个字符串时遇到问题。如有任何建议,将不胜感激
ArrayList<Weather> weatherData = new ArrayList<Weather>();
private ListView listView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (ListView) findViewById(R.id.listView1);
String[] city = {
new String("dublin,ire"),
new String("London,uk")
};
for (int i = 0; i < city.length; ++i) {
listView1.add(city[i]);
}
//String city2 = "Dublin,ire";
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Log.d("MR.bool", "Button1 was clicked ");
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
}
});
cityText = (TextView) findViewById(R.id.cityText);
condDescr = (TextView) findViewById(R.id.condDescr);
temp = (TextView) findViewById(R.id.temp);
hum = (TextView) findViewById(R.id.hum);
press = (TextView) findViewById(R.id.press);
windSpeed = (TextView) findViewById(R.id.windSpeed);
windDeg = (TextView) findViewById(R.id.windDeg);
imgView = (ImageView) findViewById(R.id.condIcon);
imgView2 = (ImageView) findViewById(R.id.imageView1);
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{city});
if (city.contains("uk")) {
imgView2.setImageResource(R.drawable.uk);
} else if (city.contains("ire")) {
imgView2.setImageResource(R.drawable.ireland);
} else if (city.contains("de")) {
imgView2.setImageResource(R.drawable.germany);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
try {
weather = JSONWeatherParser.getWeather(data);
// Let's retrieve the icon
weather.iconData = ((new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
if (weather.iconData != null && weather.iconData.length > 0) {
Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
imgView.setImageBitmap(img);
}
else if(weatherData.size() > 0)
{
ArrayAdapter<Weather> adapter = new ArrayAdapter<Weather>(MainActivity.this,
android.R.layout.activity_list_item, weatherData);
listView1.setAdapter(adapter);
// here you can also define your custom adapter and set it to listView
//according to your own defined layout as items
}
cityText.setText(weather.location.getCity() + "," + weather.location.getCountry());
condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");
hum.setText("" + weather.currentCondition.getHumidity() + "%");
press.setText("" + weather.currentCondition.getPressure() + " hPa");
windSpeed.setText("" + weather.wind.getSpeed() + " mps");
windDeg.setText("" + weather.wind.getDeg() + "�");
}
}
}
ArrayList weatherData=new ArrayList();
私有列表视图列表视图1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1=(ListView)findViewById(R.id.listView1);
字符串[]城市={
新字符串(“爱尔兰都柏林”),
新字符串(“英国伦敦”)
};
对于(int i=0;i0){
位图img=BitmapFactory.decodeByteArray(weather.iconda,0,weather.iconda.length);
设置图像位图(img);
}
else if(weatherData.size()>0)
{
ArrayAdapter=新的ArrayAdapter(MainActivity.this,
android.R.layout.activity\u list\u项目,weatherData);
listView1.setAdapter(适配器);
//在这里,您还可以定义自定义适配器并将其设置为listView
//根据您自己定义的布局作为项目
}
cityText.setText(weather.location.getCity()+”,“+weather.location.getCountry());
condDescr.setText(weather.currentCondition.getCondition()+”(“+weather.currentCondition.getDescr()+”);
temp.setText(“+Math.round((weather.temperature.getTemp()-273.15))+”�(C);;
hum.setText(“+weather.currentCondition.get湿度()+”%);
press.setText(“+weather.currentCondition.getPressure()+“hPa”);
windSpeed.setText(“+weather.wind.getSpeed()+”mps”);
windDeg.setText(“+weather.wind.getDeg()+”�");
}
}
}
您可以使用Gson解析json。
我假设您正在接收jsonArray。因此,您可以这样做:
ArrayList<Weather> weatherArrayList = new ArrayList<Weather>();
Gson gson = new Gson();
JsonParser jsonParser = new JsonParser();
JsonArray jArray = jsonParser.parse(responseStr).getAsJsonArray();
for (JsonElement obj : jArray) {
Weather weatherModel = gson.fromJson(response, Weather.class);
weatherArrayList.add(weatherModel);
}
ArrayList weatherArrayList=new ArrayList();
Gson Gson=新的Gson();
JsonParser JsonParser=新的JsonParser();
JsonArray jArray=jsonParser.parse(responsest.getAsJsonArray();
for(JsonElement对象:jArray){
Weather weatherModel=gson.fromJson(响应,Weather.class);
weatherArrayList.add(weatherModel);
}
上面的代码解析json中的数据,并将它们添加到“weatherArrayList”中。您应该将其放置在适配器中,然后在listview中进行设置
这可能是您的自定义适配器
public class WeatherAdapter extends ArrayAdapter<Weather> {
public WeatherAdapter(Context context) {
super(context, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
// here you should set up the custom view. That is the view of each Item of your ListView
}
}
公共类WeatherAdapter扩展了ArrayAda