使用游标适配器获取列表视图中的歌曲
Using cursor Adapter to get songs in a list view
我想将歌曲标题从媒体商店获取到列表视图,谁能解释一下代码中的错误?
public class MainActivity extends AppCompatActivity {
Cursor cursour;
ContentResolver cr = this.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cur = cr.query(uri, null, selection, null, sortOrder);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {
"MediaStore.Audio.Media.EXTERNAL_CONTENT_URI"
},
new int[] {
android.R.id.text1
}, 0);
ListView lv = (ListView) findViewById(R.id.songlist);
lv.setAdapter(adapter);
}
}
使用它可以将歌曲放入列表视图
public void getAudioList() {
String orderBy = MediaStore.Audio.Media.TITLE ;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media._ID }, selection, null, orderBy);
int count = mCursor.getCount();
while (mCursor.moveToNext()) {
mSongsList.add(mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
SongsPath.add(mCursor.getLong(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
}
mCursor.close();
}
lv = (ListView) findViewById(R.id.list);
getAudioList();
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,mSongsList);
lv.setAdapter(arrayAdapter);
我想将歌曲标题从媒体商店获取到列表视图,谁能解释一下代码中的错误?
public class MainActivity extends AppCompatActivity {
Cursor cursour;
ContentResolver cr = this.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cur = cr.query(uri, null, selection, null, sortOrder);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {
"MediaStore.Audio.Media.EXTERNAL_CONTENT_URI"
},
new int[] {
android.R.id.text1
}, 0);
ListView lv = (ListView) findViewById(R.id.songlist);
lv.setAdapter(adapter);
}
}
使用它可以将歌曲放入列表视图
public void getAudioList() {
String orderBy = MediaStore.Audio.Media.TITLE ;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media._ID }, selection, null, orderBy);
int count = mCursor.getCount();
while (mCursor.moveToNext()) {
mSongsList.add(mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
SongsPath.add(mCursor.getLong(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
}
mCursor.close();
}
lv = (ListView) findViewById(R.id.list);
getAudioList();
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,mSongsList);
lv.setAdapter(arrayAdapter);