Skip to content

Instantly share code, notes, and snippets.

@msomu
Created January 2, 2015 04:32
Show Gist options
  • Select an option

  • Save msomu/06436716a85e6bdfb15e to your computer and use it in GitHub Desktop.

Select an option

Save msomu/06436716a85e6bdfb15e to your computer and use it in GitHub Desktop.
How to create a list view
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Once the root view for the fragment has been created
//the ListView with some dummy data
//Create some dummy data for the list view
String [] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow -Foggy - 70/40",
"Weds - Clody - 72/63",
"Fri - Heavy Rain - 65/56",
"Sat - HELP TRAPPED IN WEATHERSTATION - 60/51",
"Sun - Sunny - 80/68",
};
List<String> weekForecast = new ArrayList<>(
Arrays.asList(forecastArray)
);
//Now that we have some dummy forecast data, create an ArrayAdapter.
//The ArrayAdapter will take data from a source (like our dummy forecast)
//use it to populate the ListView it's attached to.
mForecastAdapter = new ArrayAdapter<String>(
//The current context (this fragment's parent activity)
getActivity(),
//ID of the list item layout
R.layout.list_item_forecast,
//ID of the textview to populate
R.id.list_item_forecast_textview,
//Forecast data
weekForecast
);
//Get a reference to the ListView, and attach it to this adapter
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment