gpt4 book ai didi

java - 如何在Android中使用List在ListView中设置数据
转载 作者:行者123 更新时间:2023-12-02 04:17:18 32 4
gpt4 key购买 nike

我已向 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 列表中获取值,但不知道如何将该对象转换为字符串?

最佳答案

您可以在 ForecastWeather 内创建一个函数它将 List<SomeObject> 转换为到 listString .

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
}

关于java - 如何在Android中使用List<Object>在ListView中设置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181999/

32 4 0