final List<String> bandlist = asList("Deichkind", "Metallica","Queen","U2");
ArrayAdapter<String> bandArray = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,bandlist);
setListAdapter(bandArray);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Example code for initializing a SimpleAdapter
ListView lv = (ListView) findViewById(R.id.listview);
// create content items
String[] from = new String[]{"rowid","col_1","col_2"};
int[] to = new int[]{R.id.item1, R.id.item2, R.id.item3};
// prepare the list of all records
List<HashMap<String, String>> fillMaps =
new ArrayList<HashMap<String, String>>();
for(int i = 0; i<10;i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", ""+i);
map.put("col_1", "col_1_item_"+i);
map.put("col_2", "col_2_item_"+i);
fillMaps.add(map);
}
// fill in the layout
SimpleAdapter adapter = new SimpleAdapter(this,fillMaps,
R.layout.simple_adapter_layout, from, to);
lv.setAdapter(adapter);
content://<authority>/<table>/[<id>]
content://contacts/people/10
content://media/external/audio/media
_ID | ARTIST | TITLE | DATA | DISPLAY_NAME | DURATION |
---|---|---|---|---|---|
12 | Lithium Dawn | Everything Is Zero | storage/sdcard/01 01 Everything Is Zero.mp3 | 01 01 Everything Is Zero.mp3 | 396720 |
13 | Blur | Fool’s Day | storage/sdcard/Blur - Fool´s Day.mp3 | Blur - Fool´s Day.mp3 | 207047 |
14 | pornophonique | sad robot | storage/sdcard/sad robot.mp3 | sad robot.mp3 | 312059 |
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, //URI to CP
projection, //Array defining the data to select
selection, //String defining the criteria of the data selection
selectArgs, //parametes for the data selection
sortOrder); //specifies the result order
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
String selection = MediaStore.Audio.Media.ARTIST + " LIKE ?";
String[] selectionArgs = new String[]{"blur"};
String[] selectionArgs = new String[]{"blur"};
String sortOrder = MediaStore.Audio.Media.DURATION;
Parameter | Description |
---|---|
URI | The URI, using the content:// scheme, for the content to retrieve. |
Projection | A list of which columns to return. Passing null will return all columns, which is inefficient. |
Selection | A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI. |
selectionArgs | You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings. |
sortOrder | How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. |
final List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()){
songs.add(cursor.getString(1));
}
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM}, null, null, null);
//define which content should be placed where in the layout
String[] from = new String[]{MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM};
int[] to = new int[]{R.id.artist,R.id.album};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.artist_list,
cursor, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//artistList is the ListView object
artistList.setAdapter(adapter);