如何使用android中的List <object>在ListView中设置数据

时间:2015-10-17 02:09:56

标签: java android listview

我已将一些虚拟数据添加到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如何将该对象转换为字符串?

2 个答案:

答案 0 :(得分:2)

您可以在v_fName := WebUtil_File.File_Open_Dialog( directory_name => 'C:\' ,File_Filter => null ,Title => 'Select Client filename to Open.' ); 内创建一个功能,将ForecastWeather转换为List<SomeObject>的{​​{1}}。

list

内部String类:

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";
}

答案 1 :(得分:0)

在ForecastWeather中,声明:

    @Override
public String toString() {
    // TODO Auto-generated method stub
    return city;

}