![](/img/trans.png)
[英]How to get Json Array in Json Object using volley in android and set this json data in listview code and Xml Avilavble
[英]how to set data in ListView using List<Object> in android
我已經為ListView添加了一些虛擬數據
List<String> sevenDay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastList = {
"Today",
"Tomorrow",
"The day after Tomorrow"
};
sevenDay = new ArrayList<>(Arrays.asList(forecastList));
ArrayAdapter<String> forecasteAdapter;
forecasteAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textView,
sevenDay);
ListView forecasteListView = (ListView) rootView.findViewById(R.id.listView_forecast);
forecasteListView.setAdapter(forecasteAdapter);
new FetchWeatherTask().execute("SomeThing",null,null);
return rootView ;
}
FetchWeatherTask如下所示
public class FetchWeatherTask extends AsyncTask<String, Void, ForecastWeather>{
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
@Override
protected ForecastWeather doInBackground(String... params) {
try{
ForecastWeather forecastWeather = Util.newInstance().getForcastWeatherByCityName(params[0]);
Log.i(LOG_TAG, "Got City name " + forecastWeather.getCity().getName());
return forecastWeather;
} catch (Exception e){
Log.e(LOG_TAG, "Exception " + e.getMessage());
e.printStackTrace();
}
return null;
}
}
getForcastWeatherByCityName(params [0])返回一個對象
public class ForecastWeather {
private String cod;
private String message;
private Integer cnt;
private City city;
private java.util.List<SomeObject> list = new ArrayList<SomeObject>();
// omitting setters/getters
}
現在在onPostExecute()
我必須將sevenDay
變量更新為
@Override
protected void onPostExecute(ForecastWeather forecastWeather) {
super.onPostExecute(forecastWeather);
sevenDay = forecastWeather.getList(); // Here
}
在這里,我需要從ForecastWeather的java.util.List列表中獲取值,但是沒有Idea如何將該對象轉換為字符串?
您可以在ForecastWeather
內部創建一個將List<SomeObject>
轉換為String
list
的函數。
public class ForecastWeather {
private String cod;
private String message;
private Integer cnt;
private City city;
private java.util.List<SomeObject> list = new ArrayList<SomeObject>();
public List<String> getAsStringList(){
List<String> l=new ArrayList<>();
for(SomeObject someObject:list){
//You can override the toString() function of SomeObject class
l.add(someObject.toString());
}
return l;
}
}
在SomeObject
類中:
public class SomeObject {
//class definition etc
//override this method
@Override
public String toString() {
// put your logic to get a string for the given object. this depends upon you implementation
return "dummy";
}
}
然后在onPostExecute
@Override
protected void onPostExecute(ForecastWeather forecastWeather) {
super.onPostExecute(forecastWeather);
sevenDay = forecastWeather.getAsStringList(); // getting a string list
}
在ForecastWeather類中,聲明:
@Override
public String toString() {
// TODO Auto-generated method stub
return city;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.