[英]Simple ListView as fragment doesn't appear
我嘗試將ListView添加到我的MainActivity中,但我不知道為什么它不會出現在App中。
我創建了一個名為“list_item_forecast.xml”的新布局資源文件 - 在那里我放置了一個帶有id:“list_item_forecast_textview”的TextView。
在“content_main.xml”中我有ListView:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast" />
然后我在MainActivity類中有這個類:
public static class PlaceholderFragment extends Fragment{
ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment(){
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.content_main, container,false);
String[] forecastArray = {
"Heute - Sonnig - 34°",
"Morgen - Sonnig - 30°",
"Montag - Regen - 10°",
"Dienstag - Bewölkt - 20°",
"Mittwoch - Schnee - 10°",
"Donnerstag - Schnee - 15°",
"Freitag - Sonne - 33°"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(getContext(), 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;
}
}
這是整個項目: https : //github.com/Zaniyar/sunshine/tree/master/app/src/main
我必須在PlaceholderFragment.java中將“getContext()”更改為“this.getActivity()”:
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView listView = (ListView) rootView.findViewById(
R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.