ListView的空白屏幕

时间:2016-04-07 09:22:59

标签: java android

我正在尝试使用Fragments / ListView / Arrays& amp;适配器。 但是当我运行应用程序时,我得到了空白屏幕,请指导::

下面的

是Fragment class ::

中的MainActivity.Java代码
/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootView= inflater.inflate(R.layout.fragment_main, container, false);


    String[] forecastArray = {
            "Today , Sunny , 88/63",
            "Tomorrow , Sunny , 88/63",
            "Wesneday , Sunny , 88/63",
            "THursday , Sunny , 88/63",
            "FRIDAY , Sunny , 88/63",
            "Saturday , Sunny , 88/63",
            "Sunday , Sunny , 88/63",
    };

    List<String> weekForecast = new ArrayList<String>(
            Arrays.asList(forecastArray)
    );

        ArrayAdapter<String> mForecastAdapter;
        mForecastAdapter = new ArrayAdapter<String>(
                    getActivity(),
                    R.layout.list_item_forecast,
                    R.id.List_item_forecast_textView, weekForecast);
        ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
        listView.setAdapter(mForecastAdapter);
    return rootView;

}

1 个答案:

答案 0 :(得分:0)

也许是因为你在onCreateView中填充你的适配器。 尝试在onResume上进行。

保留对rootView的引用,并在onResume上执行此部分

ArrayAdapter<String> mForecastAdapter;
    mForecastAdapter = new ArrayAdapter<String>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.List_item_forecast_textView, weekForecast);
    ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
    listView.setAdapter(mForecastAdapter);