ContentProvider--实现在本地获取音乐显示在ListView

我用的逍遥安卓,数据库的路径:在data/data/com.android.providers.media/database/external(手机SDK)

效果:

ContentProvider--实现在本地获取音乐显示在ListView_第1张图片

实现步骤:

1.创建一个类继承Adapter(由于获取的东西比较多,所以我们可以创建一个对象)

2.在Activity中实现对应的方法

创建对象:

public class MusicContent {
    String _data,_display_name,mime_type,title,artist,album;
    long _size,date_added,duration;

    public MusicContent(String _data, String _display_name, String mime_type, String title, String artist, String album, long _size, long date_added, long duration) {
        this._data = _data;
        this._display_name = _display_name;
        this.mime_type = mime_type;
        this.title = title;
        this.artist = artist;
        this.album = album;
        this._size = _size;
        this.date_added = date_added;
        this.duration = duration;
    }

    public String get_data() {
        return _data;
    }

    public void set_data(String _data) {
        this._data = _data;
    }

    public String get_display_name() {
        return _display_name;
    }

    public void set_display_name(String _display_name) {
        this._display_name = _display_name;
    }

    public String getMime_type() {
        return mime_type;
    }

    public void setMime_type(String mime_type) {
        this.mime_type = mime_type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public long get_size() {
        return _size;
    }

    public void set_size(long _size) {
        this._size = _size;
    }

    public long getDate_added() {
        return date_added;
    }

    public void setDate_added(long date_added) {
        this.date_added = date_added;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }
}
MusicAdapte:

public class MusicAdapter extends BaseAdapter {
    Context context;
    LayoutInflater mInflater;
    ArrayList arrayList;

    public MusicAdapter(Context context, ArrayList arrayList) {
        this.context = context;
        this.arrayList = arrayList;
        this.mInflater = LayoutInflater.from(context);
    }

    @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) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.activity_main, null);
            TextView tv = (TextView) convertView.findViewById(R.id.tv);
            viewHolder = new ViewHolder(tv);
            convertView.setTag(viewHolder);
        }
        viewHolder = (ViewHolder) convertView.getTag();
        MusicContent musicContent = (MusicContent) getItem(position);
        viewHolder.textView.setText(musicContent.get_data());
        return convertView;
    }
}

class ViewHolder {
    TextView textView;

    public ViewHolder(TextView textView) {
        this.textView = textView;
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {
    ContentResolver resolver;
    ArrayList arrayList;
    ListView lv;
    Button but;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        lv = (ListView) findViewById(R.id.lv_music);
        arrayList = new ArrayList<>();
        resolver = getContentResolver();

        /**
         * 参数一:Uri uri
         * 参数二:String[] projection  列名
         * 参数三:String selection 查询条件
         * 参数四:String[] selectionArgs  查询条件的值
         * 参数五:String sortOrder 排序
         */
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String _date = cursor.getString(cursor.getColumnIndex("_data"));
                String _display_name = cursor.getString(cursor.getColumnIndex("_display_name"));
                String mime_type = cursor.getString(cursor.getColumnIndex("mime_type"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String artist = cursor.getString(cursor.getColumnIndex("artist"));
                String album = cursor.getString(cursor.getColumnIndex("album"));
                long _size = cursor.getInt(cursor.getColumnIndex("_data"));
                long date_added = cursor.getInt(cursor.getColumnIndex("_data"));
                long duration = cursor.getInt(cursor.getColumnIndex("_data"));
                MusicContent musicContent = new MusicContent(_date, _display_name, mime_type, title, artist, album, _size, date_added, duration);
                arrayList.add(musicContent);
            }
        }
        MusicAdapter musicAdapter = new MusicAdapter(this, arrayList);
        lv.setAdapter(musicAdapter);
    }
}
activity_mian.xml



    

activity_listview.xml






这里我只是简单的在ListView显示了一个属性,写得比较粗糙,大致实现是这样的





你可能感兴趣的:(Android)