Audio file is not playing - android

I am creating one application which record voice and store it in SD card. then i open
this file and want to play or some other option by clicking on that respective file.
But when i click on that list item it'd not playing. I don't understand where is the problem. Here is my code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listofaudiorecord);
init_phone_music_grid();
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
Please give me any hint.
Thanks in Advance..

How about you switch the two
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
to this
mMediaPlayer = new MediaPlayer();
musiclist.setOnItemClickListener(musicgridlistener);
or put the MediaPlayer reference inside the onItemClick()

Related

Add bitmap image from video list

I tried to make a list of videos, everything was right, the list is shown and you can see in videoview. but.
1) How can I add bitmap image in the list of videos?
2) How can I change the ListView, with Gridview?
Thank you
Videoscan code:
public class VideoScan extends AppCompatActivity{
private Cursor videocursor;
private int video_column_index;
ListView videolist;
int count;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_scan);
init_phone_video_grid();
}
private void init_phone_video_grid() {
System.gc();
String[] proj = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = videocursor.getCount();
videolist = (ListView) findViewById(R.id.PhoneVideoList);
videolist.setAdapter(new VideoAdapter(getApplicationContext()));
videolist.setOnItemClickListener(videogridlistener);
}
private AdapterView.OnItemClickListener videogridlistener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videocursor.moveToPosition(position);
String filename = videocursor.getString(video_column_index);
Intent intent = new Intent(VideoScan.this, ViewVideo.class);
intent.putExtra("videofilename", filename);
startActivity(intent);
}
};
public class VideoAdapter extends BaseAdapter {
private Context vContext;
public VideoAdapter(Context c) {
vContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(vContext.getApplicationContext());
String id = null;
if (convertView == null) {
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(position);
id = videocursor.getString(video_column_index);
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
videocursor.moveToPosition(position);
id += " Size(KB):" + videocursor.getString(video_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
xml
<ListView
android:id="#+id/PhoneVideoList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
I modified this sample, and go now. Thank #commonsware who advised me.

How to filter songs in listView

I have a listView filled up with all songs on sdcard, can i filter them by name using an ediText or something else (like a SearchBar) and update the listview?Thanks, this is how I fill up the listview:
#SuppressWarnings("deprecation")
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Audio.Media.DURATION + ">= 120000", null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.listView1);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
String canzone=filename.replace("/storage/sdcard0/", "");
canzone=canzone.replace("Music/", "");
canzone=canzone.replace("media/", "");
canzone=canzone.replace("Ringtones/", "");
t=(TextView) findViewById(R.id.textView2);
t.setWidth(130);
t.setSingleLine(true);
t.setText(canzone);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
id = musiccursor.getString(music_column_index);
tv.setText(id);
return tv;
}
}
This is the updated code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
init_phone_music_grid(null);
//Controllo();
edittext=(EditText) findViewById(R.id.editText1);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) { }
#Override
public void afterTextChanged(Editable arg0) {
init_phone_music_grid(edittext.getText().toString());
}
});
}
#SuppressWarnings("deprecation")
private void init_phone_music_grid(String a) {
System.gc();
String sortOrder=null;
if(a!=null){
sortOrder=MediaStore.Audio.Media.TITLE +a;
}
String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Audio.Media.DURATION + ">= 120000", null, sortOrder);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.listView1);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
String canzone=filename.replace("/storage/sdcard0/", "");
canzone=canzone.replace("Music/", "");
canzone=canzone.replace("media/", "");
canzone=canzone.replace("Ringtones/", "");
t=(TextView) findViewById(R.id.textView2);
t.setWidth(130);
t.setSingleLine(true);
t.setText(canzone);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
this is how you sort stuffs from mediastore
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
if you want to sort it by artists or something else..just replace the "TITLE"part with whatever you dersire..
If you find my answer useful,accept it
Edit:Sorry i forgot where you will put this sort order thing
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Audio.Media.DURATION + ">= 120000", null, null);
in this line of node..replace the last "null" with sortorder,and you will get sorted list...

how to display music files in listview in android

Hi i need to display array of music files in listview,and when i click that music file i need play that song,i tried using below code to display songs in listview,but it showing null pointer exception in adding array to textview line,but same code working for displaying images in listview,where i did mistake any one suggest me..
public class CustomListViewExample extends Activity {
Integer[] text;
public static ArrayList<Integer> list1 = new ArrayList<Integer>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.citylist);
list1.add(R.raw.apple);
list1.add(R.raw.intro_letter_report_card);
list1.add(R.raw.intro_title_page_1);
text = list1.toArray(new Integer[list1.size()]);
ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setAdapter(new MyCustomAdapter(text));
}
class MyCustomAdapter extends BaseAdapter {
Integer[] data_image;
MyCustomAdapter() {
data_image = null;
}
MyCustomAdapter(Integer[] text) {
data_image = text;
}
#Override
public int getCount() {
return data_image.length;
}
#Override
public String getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.city_row_item, parent, false);
TextView t1=(TextView)row.findViewById(R.id.textView1);
t1.setText(""+data_image[position]);
return (row);
}
}
}
Use following code to display audio file in ListView and clicking on any one of it, plays that song
public class AudioListActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.audiolist_activity);
init_phone_music_grid();
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
System.gc();
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
mMediaPlayer.stop();
}
}
For NPE, seems there's no textView1 view in R.layout.city_row_item.
Regarding showing list of music files and playing them, You could refer to this exemple.
Also, I believe You need to refer to this video from 2010 IO for creation of better adapters for ListViews (because in your code You don't use convertView item).
Use sound pool to load the sounds into the memory first. This is how I'm doing it in my app Beat Shaker:
// Sound pool new instance
spool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
// Sound pool on load complete listener
spool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Log.i("OnLoadCompleteListener","Sound "+sampleId+" loaded.");
loaded = true;
}
});
// Load the sample IDs
soundId = new int[3];
soundId[0] = spool.load(this, R.raw.clave, 1);
soundId[1] = spool.load(this, R.raw.maracas, 1);
soundId[2] = spool.load(this, R.raw.crash, 1);
Then you can call a thread from the corresponding list selection listener that runs a function similar to this:
public void Sound1(){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int streamID = -1;
do{
streamID = spool.play(soundId[0], volume, volume, 1, 0, 1f);
} while(streamID==0);
};

How to play music from sdcard on click event of coverflow images

I followed the link for coverflow implementation
http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
Here with onclick event I want to play songs from sdcard , I mean on clicking each image of coverflow I want to play differnet songs from sd card
Please share your Ideas.
Help is always appreciated.
Thanks in advance.
coverFlow.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(CoverAdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
// play song number1
} else if (position == 1) {
// play song number 2
}
}
});
I am following this code for this
public class Entertainment extends Activity {
ListView musiclist;
TextView CurrentPlying;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
int seekBarState =0;
ImageButton rewindButton,playButton,forwardButton,stopButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entertainment);
list_of_media_files();
Gallery addsgallery = (Gallery) findViewById(R.id.addsgallery);
addsgallery.setAdapter(new AddsViewAdaptor(this));
rewindButton = (ImageButton) findViewById(R.id.rewind);
playButton = (ImageButton) findViewById(R.id.play);
forwardButton = (ImageButton) findViewById(R.id.forward);
stopButton = (ImageButton) findViewById(R.id.stop);
playButton.setOnClickListener(new PlayButtonListner());
rewindButton.setOnClickListener(new RewindButtonListner());
forwardButton.setOnClickListener(new ForwardButtonListner());
stopButton.setOnClickListener(new StopButtonListner());
}
class PlayButtonListner implements View.OnClickListener{
public void onClick(View v) {
try{
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}else{
mMediaPlayer.start();
}
}
catch(Exception ex){
Log.i("Exception", "Exception in mediaplayer e = " + ex);
}
}
}
public void onInit(int status) {
}
private void list_of_media_files() {
//System.gc();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(this));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
//System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
// CurrentPlying =(TextView) findViewById(R.id.currentSong);
//CurrentPlying.setText(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext);
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
class RewindButtonListner implements View.OnClickListener{
public void onClick(View v) {
try{
if (mMediaPlayer.isPlaying()) {
seekBarState=mMediaPlayer.getCurrentPosition();
mMediaPlayer.seekTo(seekBarState-300);
}else{
mMediaPlayer.start();
}
}
catch(Exception ex){
Log.i("Exception", "Exception in mediaplayer e = " + ex);
}
}
}
class ForwardButtonListner implements View.OnClickListener{
public void onClick(View v) {
try{
if (mMediaPlayer.isPlaying()) {
seekBarState=mMediaPlayer.getCurrentPosition();
mMediaPlayer.seekTo(seekBarState+300);
}else{
mMediaPlayer.start();
}
}
catch(Exception ex){
Log.i("Exception", "Exception in mediaplayer e = " + ex);
}
}
}
class StopButtonListner implements View.OnClickListener{
public void onClick(View v) {
try{
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.reset();
};
}
catch(Exception ex){
Log.i("Exception", "Exception in mediaplayer e = " + ex);
}
}
}
}
first make List or array of song list from sdcard then pass song using position
coverFlow.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(CoverAdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
playSong(list[position]); <---
}
});

Alphabetize queried Music Files in ListView

Ok so im making a media player app for android. everything was ok until now. so far i have a list view that shows all .mp3 files on ur sdcard(internal and external) and when playing show a music visualizer. but i cant for the life of me alphabetize the list. everything is dynamic so xml doesnt work here.
public class MusicPlayerActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
private Intent aIntent;
public static String filename;
private RelativeLayout mRelativeLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_music_grid();
mRelativeLayout = new RelativeLayout(this);
setContentView(mRelativeLayout);
mRelativeLayout.addView(musiclist);
}
public void init_phone_music_grid() {
System.gc();
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM
};
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
musiccursor = managedQuery(allsongsuri, projection , selection, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new EfficientAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
System.gc();
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
filename = musiccursor.getString(music_column_index);
aIntent = new Intent(v.getContext(), AudioFX.class);
aIntent.getStringExtra(filename);
startActivity(aIntent);
}
};
class EfficientAdapter extends BaseAdapter {
private Context mContext;
public EfficientAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
id = musiccursor.getString(music_column_index);
tv.setText(id);
tv.setTextSize(20);
return tv;
}}}
You just need to use DEFAULT_SORT_ORDER, like this:
String sortOrder = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
musiccursor = managedQuery(allsongsuri, projection , selection, null, sortOrder);
Showing the title inside the getView method won't get a alphabetized list.
Inorder to alphabetize the list get all the music titles into a String array or an ArrayList
Alphabetize them manually and pass that String array to EfficientAdapters Constructor and store it locally and use them according to the lists position.
like
class EfficientAdapter extends BaseAdapter {
private Context mContext;
private String values[];
public EfficientAdapter(Context c, String[] a) {
mContext = c;
values = a;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
id = values[position];
tv.setText(id);
tv.setTextSize(20);
return tv;
}}}
Hope this helps.....

Categories

Resources