ListView updates only when activity restarts - Android

安稳与你 提交于 2019-12-11 22:25:16

问题


Hi i have provided my code below. I have one Main UI thread and one manual thread. I am updating the listView inside the thread and using it in an adapter and list view to populate. But the problem is once the activity starts the list is empty and only when the configuration changed it gets updated with the adapter. whats the problem in it? My thread is starting only after the oncreate is complete? Kindly leave the cursor part as I am getting all the values very well. Kindly help me friends.

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");
}

}

来源:https://stackoverflow.com/questions/18673553/listview-updates-only-when-activity-restarts-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!