尝试将适配器绑定到listView,我在手机上看到一个空白屏幕

时间:2015-09-22 08:31:26

标签: android android-listview android-arrayadapter

我将一个适配器绑定到listView并且我没有收到任何错误,但是,我也没有看到输出。

我是一个非常初学者,我花了3个多小时试验这段代码并且无法提出解决方案

这是我的MainActivityFragment代码:

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String[] forecastArray = {
            "String 1",
            "String 2",
            "String 3",
            "String 4",
            "String 5",
            "String 6",
            "String 7",
            "String 8",
            "String 9"

    };
    List<String> weekForecast = new ArrayList<>(
            Arrays.asList(forecastArray)
    );
    ArrayAdapter<String> mForecastAdapter = new ArrayAdapter<>(getActivity(), R.layout.list_item_forecast,
            R.id.list_item_forecast_textView, weekForecast);

    View v = inflater.inflate(R.layout.fragment_main, container, false);
    ListView lv = (ListView) v.findViewById(R.id.listView_forecast);
    lv.setAdapter(mForecastAdapter);

    return inflater.inflate(R.layout.fragment_main, container, false);
}

2 个答案:

答案 0 :(得分:1)

你必须退回你的膨胀视图。

 public MainActivityFragment() {
  }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
         String[] forecastArray = {
            "String 1",
            "String 2",
            "String 3",
            "String 4",
            "String 5",
            "String 6",
            "String 7",
            "String 8",
            "String 9"

    };
    List<String> weekForecast = new ArrayList<>(
            Arrays.asList(forecastArray)
    );
    ArrayAdapter<String> mForecastAdapter = new ArrayAdapter<>(getActivity(), R.layout.list_item_forecast,
            R.id.list_item_forecast_textView, weekForecast);

    View v = inflater.inflate(R.layout.fragment_main, container, false);
    ListView lv = (ListView) v.findViewById(R.id.listView_forecast);
    lv.setAdapter(mForecastAdapter);




       return v;
    }

答案 1 :(得分:0)

我认为您应该return v;而不是返回inflater.inflate(R.layout.fragment_main, container, false);