Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Created July 17, 2016 10:20
Show Gist options
  • Select an option

  • Save Bahaaib/62ce4078e154cf34c39a77e401d2b483 to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/62ce4078e154cf34c39a77e401d2b483 to your computer and use it in GitHub Desktop.
How to initialize A List View
ListView myList = (ListView) findViewById(R.id.mylist)
ArrayList<String> myFamily = new ArrayList<String>();
myFamily.add("XXX"); // adding new items
myFamily.add("YYY");
// OR ArrayList<String> myFamily = new ArrayList<String>(asList"XXX", "YYY");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.id.simple_list_item_1, myFamily);
myList.setAdapter(arrayAdapter);
// Adapter is to convert the ArrayList to ListView items
myList.setOnItemClickListener(new AdapterView.setOnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
// Here's where we tell the action needs to be done after tapping the item
}
}};
// The Listener is to take actions on every single tap on each of the ListView items (if desired)
/* [ AdapterView<?> ] is a Generic variable type used to handle different kinds of views like ListViews, GridViews, so the <?> cause
we don't know actually which View exactly will send the value to the method.
[View view ] Represents what actually has been taped like in this case A row in the ListView
[ int position ] Represents the position of the item (child) of the original parent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment