Reputation: 11
I am a beginner and brushing my skills in app development. I am currently doing an example where a List is being populated by String Array. In the last steps the binding takes place and this is where the method "findviewbyid" is not found and appears red after build. Can someone guide me on this?
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<String> weather = new ArrayList<String>();
weather.add("Today-Sunny-80/23");
weather.add("Tomorrow-Sunny-83/23");
weather.add("Fri-Sunny-80/23");
weather.add("Sat-Rainy-80/23");
weather.add("Sun-Sunny-80/23");
weather.add("Mon-Foggy-80/23");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weather);
ListView listView = (ListView) findViewByID(R.id.listView_Forecast);
listView.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_main, container, false);
Upvotes: 0
Views: 179
Reputation: 12173
First of all findViewByID
should be findViewById
(lowercase d
)
For a fragment, set up your views in onCreateView
like this
// Call the layout inflater first
View view = inflater.inflate(R.layout.fragment_main, container, false);
// Initialize your views
ListView listView = (ListView) view.findViewById(R.id.listView_Forecast);
listView.setAdapter(adapter);
// Do the rest of your code
...
return view;
Upvotes: 2
Reputation: 1808
change
return inflater.inflate(R.layout.fragment_main, container, false);
to
View view= inflater.inflate(R.layout.fragment_main, container, false);
then add your code to initialize your listview as below...
ListView listView = (ListView) view.findViewById(R.id.listView_Forecast);
listView.setAdapter(adapter);
Upvotes: 0