Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ListView Data From Service Class Android

I want to display listview in my songlist activity,.. For that i have created MyService class that i am going to use for playback service. In that MyService class i have created method that will get al the songs available on SDCard. I want to display all that song in listview of songlist Activity by using service.

Here is my code in songList Activity :

private ServiceConnection serviceConnection = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName name) {
            objservice = null;
        }

        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            objservice = ((MyService.LocalBinder) service).getService();
            adapter = (ListAdapter) objservice.getSongList();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.playlist);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        bindService(new Intent(PlayListActivity.this, MyService.class),
                serviceConnection, BIND_AUTO_CREATE);

        setListAdapter(adapter);
       }

Service class code :

public class MyService extends Service {

    private MediaPlayer mp = null;
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private ListAdapter adapter = null;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return new LocalBinder();
    }

    @Override
    public void onCreate() {

    }

    public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    public ListAdapter getSongList() {
        final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

        Cursor cursor = getContentResolver().query(
                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, null);
        if (cursor == null) {
            // Query Failed , Handle error.
        } else if (!cursor.moveToFirst()) {
            // No media on the device.
        } else {
            int titleColumn = cursor
                    .getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = cursor
                    .getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
            int artistcolumn = cursor
                    .getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
            int durationcolumn = cursor
                    .getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
            int id = cursor
                    .getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
            for (int i = 0; i < cursor.getCount(); i++) {
                String thisTitle = cursor.getString(titleColumn);
                String path = cursor.getString(idColumn);
                String artist = cursor.getString(artistcolumn);
                Long duration = cursor.getLong(durationcolumn);
                Utilities objUtilities = new Utilities();
                String timeDuration = objUtilities
                        .milliSecondsToTimer(duration);
                String ID = cursor.getString(id);

                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", thisTitle);
                song.put("songPath", path);
                song.put("artist", artist);
                song.put("duration", timeDuration);
                song.put("id", ID);
                songsList.add(song);
                cursor.moveToNext();
            }
        }

        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);
            // adding HashList to ArrayList
            songsListData.add(song);
        }

        String[] from = { "songTitle", "artist", "duration" };
        int[] to = { R.id.songTitle, R.id.songArtist, R.id.duration };
        return adapter = new SimpleAdapter(this, songsListData,
                R.layout.playlist_item, from, to) {
            public long getItemId(int position) {
                return songsListData.indexOf(getItem(position));
            }
        };
    }
}

So what is my error?

like image 677
Zankhna Avatar asked Dec 07 '25 06:12

Zankhna


1 Answers

My problem has been solved. I have created one method that will set adapter to my listview. i have called that method ater connected to my service :

public void updateListView() {
        setListAdapter(adapter);
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                int songIndex = (int) id;
                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidBuildingMusicPlayerActivity.class);
                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);
                in.putExtra("listFlag", 0);
                startActivity(in);

            }
        });
    }

call this method here :

public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            objservice = ((MyService.LocalBinder) service).getService();
            adapter = (ListAdapter) objservice.getSongList();
            updateListView(); // here was my problem
        }

This is the way you can update listview by using service . Thanx.

like image 167
Zankhna Avatar answered Dec 09 '25 21:12

Zankhna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!