Skip to content

Instantly share code, notes, and snippets.

@chrisyeung1121
Created March 24, 2015 15:49
Show Gist options
  • Save chrisyeung1121/64cfce11b4af8a1ef3c2 to your computer and use it in GitHub Desktop.
Save chrisyeung1121/64cfce11b4af8a1ef3c2 to your computer and use it in GitHub Desktop.
Android: Add String Array to ArrayAdapter
// Created in CarsFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_cars, container, false);
String[] dummyCarData = {
"BMW 1",
"BMW 2",
"BMW 3",
"BMW 4",
"BMW 5"
};
List<String> carArray = Arrays.asList(dummyCarData);
ArrayAdapter<String> carListAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_car,
R.id.list_item_car_textView,
carArray);
ListView listView = (ListView) rootView.findViewById(R.id.listView_cars);
listView.setAdapter(carListAdapter);
return rootView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment