Filling an adapter view with data in android have two methods.
You can populate an
Android provides several subclasses of
2. SimpleCursorAdapter
1. ArrayAdapter
Use this adapter when your data source is an array. By default,
For example, if you have an array of strings you want to display in a
Then simply call
To customize the appearance of each item you can override the
Example 1 Example 2 Example 3
2. SimpleCursorAdapter
Example 1 Example 2 Example 3
You can populate an
AdapterView
such as ListView
or GridView
by binding the AdapterView
instance to an Adapter
, which retrieves data from an external source and creates a View
that represents each data entry.Android provides several subclasses of
Adapter
that are useful for retrieving different kinds of data and building views for an AdapterView
. T1. ArrayAdapter
2. SimpleCursorAdapter
1. ArrayAdapter
Use this adapter when your data source is an array. By default,
ArrayAdapter
creates a view for each array item by calling toString()
on each item and placing the contents in a TextView
. For example, if you have an array of strings you want to display in a
ListView
, initialize a new ArrayAdapter
using a constructor to specify the layout for each string and the string array:ArrayAdapter adapter = new ArrayAdapter<String>(this,The arguments for this constructor are:
android.R.layout.simple_list_item_1, myStringArray);
Then simply call
setAdapter()
on your ListView
:ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
To customize the appearance of each item you can override the
toString()
method for the objects in your array. Or, to create a view for each item that's something other than a TextView
(for example, if you want an ImageView
for each array item), extend the ArrayAdapter
class and override getView()
to return the type of view you want for each item.Example 1 Example 2 Example 3
2. SimpleCursorAdapter
- Use this adapter when your data comes from a
Cursor
. When usingSimpleCursorAdapter
, you must specify a layout to use for each row in theCursor
and which columns in theCursor
should be inserted into which views of the layout. For example, if you want to create a list of people's names and phone numbers, you can perform a query that returns aCursor
containing a row for each person and columns for the names and numbers. You then create a string array specifying which columns from theCursor
you want in the layout for each result and an integer array specifying the corresponding views that each column should be placed:String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
When you instantiate the
ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};SimpleCursorAdapter
, pass the layout to use for each result, theCursor
containing the results, and these two arrays:SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
The
R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);SimpleCursorAdapter
then creates a view for each row in theCursor
using the provided layout by inserting eachfromColumns
item into the correspondingtoViews
view.
.
notifyDataSetChanged()
. This will notify the attached view that the data has been changed and it should refresh itself.Example 1 Example 2 Example 3
Handling click events
You can respond to click events on each item in anAdapterView
by implementing the AdapterView.OnItemClickListener
interface. For example:// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Do something in response to the click
}
};
listView.setOnItemClickListener(mMessageClickedHandler);
Sources: http://developer.android.com/guide/topics/ui/declaring-layout.html