程序开始会自动读取sdcard的媒体文件的信息,然后放到adapter里面,这个没有问题。我加了一个手动刷新媒体文件的功能,目的就是手动读取媒体文件的信息然后更新到adapter里面,接着listview会立刻刷新数据。现在listview不能立刻刷新,必须要退出程序再次进入才行
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabContentFactory;public class doorActivity extends TabActivity implements
TabHost.TabContentFactory {
private static final String TAG="doorActivity";

private TabHost mTabHost;
private ListView mListView;
private  MusicListAdapter adapter; private static final int refresh_music = Menu.FIRST;
private static final int out_prag = Menu.FIRST + 1;

private static final int handler_list = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("list").setIndicator("音乐列表",
getResources().getDrawable(R.drawable.item)).setContent(this));

} private void setListData() {
Cursor c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE, //音乐名
MediaStore.Audio.Media.DURATION, //音乐总时间
MediaStore.Audio.Media.ARTIST,  //艺术家
MediaStore.Audio.Media._ID, //音乐id
MediaStore.Audio.Media.DISPLAY_NAME, //音乐文件名
MediaStore.Audio.Media.DATA, //音乐路径
MediaStore.Audio.Media.ALBUM_ID }, //专辑Id
null, null, null);
Log.d(TAG, "c.getCount()="+c.getCount());
if (c == null || c.getCount() == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("存储列表为空").setPositiveButton("确定", null);
AlertDialog ad = builder.create();
ad.show();
return;
}


adapter = new MusicListAdapter(this, c);
mListView.setAdapter(adapter);

} @Override
public View createTabContent(String tag) {
if (tag.equals("list")) {
mListView = new ListView(this);
setListData();
}
return mListView;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, refresh_music, 0, "刷新库");
menu.add(0, out_prag, 1, "退出");
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){
case refresh_music:
scanSdCard();
break;
default:
break;
}
return true;
}

private void scanSdCard(){
IntentFilter intentfilter = new IntentFilter( Intent.ACTION_MEDIA_SCANNER_STARTED);
     intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
     intentfilter.addDataScheme("file");    
     registerReceiver(scanSdReceiver, intentfilter);
     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
     intent.setData(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()));
     Log.d(TAG,intent.getData().toString());
     sendBroadcast(intent);
    
}

private BroadcastReceiver scanSdReceiver = new BroadcastReceiver(){
public static final String TAG = "ScanSdReceiver";
private AlertDialog.Builder builder;
private AlertDialog ad;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG,action);
if(Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)){ builder = new AlertDialog.Builder(context);
builder.setMessage("正在扫描sdcard...");
ad = builder.create();
ad.show();
adapter.notifyDataSetChanged();
}else if(Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)){
ad.cancel();
Toast.makeText(context, "扫描完毕", Toast.LENGTH_SHORT).show();



}



}
};
}

解决方案 »

  1.   

    http://www.4ucode.com/Study/Topic/910141
      

  2.   

    adapter = new MusicListAdapter(this, c);创建 adapter时传的是cursor进去(是用cursor的数据填充listview),当你扫描完sdcard ,你并没有改变你的cursor啊,所以不管你咋个notifydatesetchange都不会有效果。你应该重新再cursor一次,然后把全来的cursor替换掉,就可以看到刷新效果。
      

  3.   

    我根据你提供的思路加了几句代码,还是不行。就是在broadcast里面的if括号分支内最后加了几行adapter = new MusicListAdapter(getApplicationContext(),c)//这个cursor是我重新获取的新的
    mListView.setAdapter(adapter);
      

  4.   

    你先用log判断一下数据有么有变化啊