listview 如何删除sdcard卡文件并刷新界面?红色部分

package com.genny;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.*;

public class MusicListActivity extends Activity{
private ListView listview;
private Cursor c = null;
private File file = null;
private CursorAdapter adapter;
private ScanSdReceiver scanSdReceiver = null;

private String[] _title = null;
private String[] _name = null;
private String[] _size;
private String[] _path = null;
private String[] _singer = null;
private String[] _album = null;
private int[] _time;

private int index;

//menu菜单
private static final int ITEM1 = Menu.FIRST;
private static final int ITEM2 = Menu.FIRST + 1;
private static final int ITEM3 = Menu.FIRST + 2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.musiclist);

    listview = (ListView)findViewById(android.R.id.list);
    c = this.getContentResolver()
            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Media.TITLE,//歌曲名0
                    MediaStore.Audio.Media.DURATION,//时长1
                    MediaStore.Audio.Media.ARTIST,//歌手2
                    MediaStore.Audio.Media._ID,//ID3
                    MediaStore.Audio.Media.DISPLAY_NAME,//文件名4
                    MediaStore.Audio.Media.ALBUM,//专辑5
                    MediaStore.Audio.Media.SIZE,//大小6
                    MediaStore.Audio.Media.DATA},//路径7
                    null, null, null);
    /*
    if (c==null || c.getCount()==0){
        builder = new AlertDialog.Builder(this);
        builder.setMessage("存储列表为空...").setPositiveButton("确定", null);
        ad = builder.create();
        ad.show();
    }*/
    c.moveToFirst();
    _title = new String[c.getCount()];
    _name = new String[c.getCount()];
    _path = new String[c.getCount()];
    _singer = new String[c.getCount()];
    _album = new String[c.getCount()];
    _size = new String[c.getCount()];
    _time = new int[c.getCount()];

    for(int i=0;i<c.getCount();i++){
        _title[i] = c.getString(0);
        _name[i] = c.getString(4);
        _path[i] = c.getString(7);
        _singer[i] = c.getString(2);
        _album[i] = c.getString(5);
        _size[i] = c.getString(6);
        _time[i] = c.getInt(1);

        c.moveToNext();
    }

    adapter = new SimpleCursorAdapter(this, R.layout.listitem, c, 
            new String[]{MediaStore.Audio.AudioColumns.TITLE,
            MediaStore.Audio.AudioColumns.ARTIST}, 
            new int[]{R.id.music, R.id.songer});

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new ListItemClickListener());

    registerForContextMenu(listview);
}

class ListItemClickListener implements OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        // TODO Auto-generated method stub
        Intent i = new Intent(MusicPlayerService.MUSIC_CONTROL);
        i.putExtra("cmd", MusicPlayerService.CMD_JUMP);
        i.putExtra("pos", position);
        sendBroadcast(i);
        finish();
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    index = ((AdapterContextMenuInfo)menuInfo).position;//获取menu点击项的position
    menu.setHeaderIcon(R.drawable.header_icon);
    menu.setHeaderTitle(_title[index]);
    menu.add(0,ITEM1,0,"播放");
    menu.add(0,ITEM2,0,"删除");
    menu.add(0,ITEM3,0,"详细");

    super.onCreateContextMenu(menu, v, menuInfo);
}

public boolean onContextItemSelected(MenuItem item){
    switch(item.getItemId()){
    case ITEM1:
        Intent i = new Intent(MusicPlayerService.MUSIC_CONTROL);
        i.putExtra("cmd", MusicPlayerService.CMD_JUMP);
        i.putExtra("pos", index);
        sendBroadcast(i);
        finish();
        break;
    [color=red]case ITEM2:
        /*下面的代码能够删除listview中的音频文件,但是删除之后如何刷新呢?*/
        file = new File(_path[index]);
        if(file.exists()){
            file.delete();
        }
        break;[/color]
    case ITEM3:
        Intent intent = new Intent(MusicListActivity.this,MusicInfo.class);
        intent.putExtra("_Name", _name[index]);
        intent.putExtra("_Size", _size[index]);
        intent.putExtra("_Singer", _singer[index]);
        intent.putExtra("_Album", _album[index]);
        intent.putExtra("_Time", _time[index]);
        intent.putExtra("_Path", _path[index]);

        startActivity(intent);
        System.out.println("Name:"+_name[index]+"  Size:"+_size[index]+"  Path:"+_path[index]+"  Singer:"+_singer[index]+"  Album:"+_album[index]+"  Time:"+_time[index]);
        break;
    default:
        break;
    }

    return super.onContextItemSelected(item);

}

//添加menu菜单
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0,ITEM1,0,"更新列表").setIcon(R.drawable.update_file);
menu.add(0,ITEM2,0,"返回").setIcon(R.drawable.menu_exit);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case ITEM1:
            scanSdCard();
            break;
        case ITEM2:
            onBackPressed();
            break;
        default:
            break;
    }
    return true;
}

//扫描本地sdcard卡文件
private void scanSdCard(){
    IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
    intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
    intentfilter.addDataScheme("file");
    scanSdReceiver = new ScanSdReceiver();
    registerReceiver(scanSdReceiver, intentfilter);
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory().getAbsolutePath())));
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    if (scanSdReceiver!=null)
            unregisterReceiver(scanSdReceiver);
    this.finish();
    super.onBackPressed();
}

}

1个回答

重新获得c,adapter.notifyDataSetChanged();

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐