内容提供者从本地数据库获取音乐数据

内容提供者从本地数据库获取音乐数据

 

第一步:

ContentResolver resolver = getContentResolver()

获取contentResolver

 

第二步:

Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
if (cursor != null){
    while (cursor.moveToNext()){    

        String _data = cursor.getString(cursor.getColumnIndex("_data"));
        String _display_name = cursor.getString(cursor.getColumnIndex("_display_name"));
        String artist = cursor.getString(cursor.getColumnIndex("artist"));
        String album = cursor.getString(cursor.getColumnIndex("album"));
        long _size = Long.parseLong(cursor.getString(cursor.getColumnIndex("_size")));
        MusicContent musicContent = new MusicContent(_data,_display_name,artist,album,_size);
        arrayList.add(musicContent);
 

从数据库拿音乐文件

 

第三步:

public class MyAdapter extends BaseAdapter {

    Context context;
    ArrayList arrayList;

    public MyAdapter(Context context, ArrayList arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView =  LayoutInflater.from(context).inflate(R.layout.list_item,null);
        TextView textView = (TextView) convertView.findViewById(R.id.tv_item);
        textView.setText((CharSequence) arrayList.get(position));
        return convertView;
    }

 

定义Adapter给listview设置数据。

 

 

给liveview设置数据在界面上展示:

MyAdapter myAdapter = new MyAdapter(this,arrayList);
lv_view.setAdapter(myAdapter);

 

 

 

你可能感兴趣的:(android,ContentResolver,数据库)