您好我在下面提供了我的代码。我有一个主UI线程和一个手动线程。我正在更新线程中的listView并在适配器和列表视图中使用它来填充。但问题是,一旦活动开始,列表就是空的,只有当配置发生变化时才会使用适配器进行更新。它的问题是什么?我的线程只在oncreate完成后启动?请保留光标部分,因为我很好地获得了所有的值。请帮助我的朋友。
public class MediaActivity extends Activity {
private static final String TAG = null;
ExpandableListView expList ;
ExpandableListAdapter expListAdapter;
static ArrayAdapter<String> ap;
static List<String> albumHead = new ArrayList<String>();
static HashMap<String, List<String>> albumChild = new HashMap<String, List<String>>();
static Cursor albumCursor;
AlbumThread albumThread;
@Override
protected void onStart() {
super.onStart();
albumThread = new AlbumThread();
albumThread.start();
Log.d(TAG , "albumThread Started");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media);
ListView expList = (ListView)findViewById(R.id.mediaList);
ap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, albumHead);
if(!albumHead.isEmpty()){
expList.setAdapter(ap);
} else {
Toast makeToast = Toast.makeText(getApplicationContext(), "albumHead is empty" , Toast.LENGTH_LONG);
makeToast.show();
}
}
private static class AlbumThread extends Thread{
Context appContext = MediaApp.getAppContext();
List<String> songList = new ArrayList<String>();
public AlbumThread() {
super("myThread");
}
@Override
public void run() {
// Query Media Contents from MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
super.run();
ContentResolver albumResolver = appContext.getContentResolver();
Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] mediaColumns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
};
String mediaSort = " " + MediaStore.Audio.Media.ALBUM + " ASC" + "," + MediaStore.Audio.Media.DISPLAY_NAME + " ASC";
albumCursor = albumResolver.query(mediaContentUri, mediaColumns, null, null, mediaSort);
//Extract values from Cursor
if(albumCursor.moveToFirst()){
do{
int albumIdx = albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
int songIdx = albumCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
String albumString = albumCursor.getString(albumIdx);
String songString = albumCursor.getString(songIdx);
if(!albumHead.contains(albumString)){
albumHead.add(albumString);
ap.notifyDataSetChanged();
}
if(albumHead.contains(albumString)){
songList.add(songString);
albumChild.put(albumString, songList);
}
}while (albumCursor.moveToNext());
}
}
}
@Override
protected void onStop() {
super.onStop();
albumThread = null;
Log.d(TAG, "albumThread stopped yapee");
}
}