1

我正在开发在 BaseAdapter 的 getView() 方法中动态检索所有音频文件(带有专辑封面)的应用程序。

现在我如何删除列表视图中的项目。(例如,如果我单击 listView 的第 3 项,则应删除或删除第 3 项)。

音乐光标代码

String[] proj = { MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.DATA,
        MediaStore.Audio.Media.DISPLAY_NAME,
        MediaStore.Video.Media.SIZE, MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.TITLE };

Cursor musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        proj, null, null, null);
int count = musiccursor.getCount();

适配器代码:

public class MusicAdapter extends BaseAdapter {
        private Context mContext;

        public MusicAdapter(Context con) {
            mContext = con;
        }
        public int getCount() {
            return count;
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {

            String id = null;
            ViewHolder holder;
            LayoutInflater inflater = getLayoutInflater();

            if (convertView == null) {

                convertView = inflater.inflate(R.layout.sub_layout, null);
                holder = new ViewHolder();

                holder.ID_ = (TextView) convertView.findViewById(R.id.txtSongName);
                holder.SIZE_ = (TextView) convertView.findViewById(R.id.txtSongSize);
                holder.ARTIST = (TextView) convertView.findViewById(R.id.txtSongArtist);
                holder.TITLE_image = (ImageView) convertView.findViewById(R.id.imgArtWork);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // code for retrieving audio name and set to textView
            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);
            holder.ID_.setText(id);
            holder.ID_.setTextSize(15);

            // code for retrieving audioSize and set to textView
            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            musiccursor.moveToPosition(position);
            id = (musiccursor.getString(music_column_index) + " (KB)");
            holder.SIZE_.setText(id);
            holder.SIZE_.setTextSize(15);

            // code for retrieving audio_Artist name and set to textView
            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);
            holder.ARTIST.setText(id);
            holder.ARTIST.setTextSize(15);

            // code for retrieving album-art(from albumid) and set to ImageView

            musiccursor.moveToPosition(position);
            album_id_long = musiccursor.getLong(musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id_long);
            ContentResolver res = MusicActivity.this.getContentResolver();
            InputStream in = null;
            try {
                in = res.openInputStream(uri);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            Bitmap artwork = BitmapFactory.decodeStream(in);

            holder.TITLE_image.setImageBitmap(artwork);

            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    //on this code of onClick,a selected item should be deleted

                }
            });

            return convertView;
        }

        private class ViewHolder {
            TextView ID_;
            TextView SIZE_;
            TextView ARTIST;
            ImageView TITLE_image;
        }
    }

在此处输入图像描述

如果有另一种方法可以做到这一点。这将不胜感激。

谢谢。

4

2 回答 2

1

您真的要删除音乐文件还是只是将其从 ListView 中删除?对了,你只需要调用“musiccursor.moveToPosition(position);” 一开始

于 2013-06-03T12:15:37.633 回答
1

你必须覆盖

public void onListItemClick(ListView l, View v, int position, long id)

当您单击视图时,将调用此回调。当它被调用时,您可以从数据库中删除该条目,然后重新运行您创建的查询以填充您的ListView. 如果您正在扩展Activity而不是ListActivity,则必须设置OnItemClickListener

yourListView.setOnItemClickListener(
    new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long id) {


             }
        }
 );
于 2013-05-28T10:23:31.197 回答