The code
SngList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView a, View v, int position, long id) {
Intent intent = new Intent(getActivity(), NowPlaying.class);
intent.putExtra("Data1",Songinfo);
intent.putExtra("Data2",position);
startActivity(intent);
}
});
code in the receiving class
Intent i = getIntent();
ArrayList<SongDetails> Songinfo2 = (ArrayList<SongDetails>)i.getSerializableExtra("Data1");
position=i.getIntExtra("Data2", 1);
code for songDetials
package sourcecode.jazzplayer;
import java.io.Serializable;
import android.graphics.Bitmap;
public class SongDetails implements Serializable{
Bitmap icon ;
String song;
String Artist;
String Album;
String Path;
int icLauncher;
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public String getArtist() {
return Artist;
}
public void setArtist(String Artist) {
this.Artist = Artist;
}
public Bitmap getIcon() {
return icon;
}
public void setIcon(Bitmap bitmap) {
this.icon = bitmap;
}
public String getPath2() {
return Path;
}
public void setPath2(String Path) {
this.Path = Path;
}
public String getAlbum() {
return Album;
}
public void setAlbum(String Album) {
this.Album = Album;
}
public void setIcon(int icLauncher) {
this.icLauncher = icLauncher;
}
}
the whole code:
public class FragmentSongs extends Fragment implements Serializable {
AdapterView.AdapterContextMenuInfo info;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_song, container, false);
ListView SngList = (ListView) view.findViewById(R.id.SongList);
registerForContextMenu(SngList);
File f=new File("/system/");
//File f=new File("/sdcard/Music");
int j=0;int i=0;
final ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);
if (Songinfo.size()>0)
{
for( j=0; j<Songinfo.size();j++)
{
for ( i=j+1 ; i<Songinfo.size(); i++)
{
SongDetails a=Songinfo.get(i);
SongDetails b=Songinfo.get(j);
if(a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase())<0)
{
Songinfo.set(i,b );
Songinfo.set(j,a);
}
}
}
SngList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView a, View v, int position, long id) {
Intent intent = new Intent(getActivity(), NowPlaying.class);
intent.putExtra("Data1",Songinfo);
intent.putExtra("Data2",position);
startActivity(intent);
}
});
SngList.setAdapter(new CustomAdapter(Songinfo));
return view;
}
else return null;
}
public ArrayList<SongDetails> getSongsFromDirectory(File f)
{MediaMetadataRetriever mmr = new MediaMetadataRetriever();
ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
Bitmap bitmap2;
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ab);
float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
bitmap2 = Bitmap.createScaledBitmap(bmp, (int) ht_px, (int) wt_px, true);
byte[] rawArt = null;
Bitmap art;
BitmapFactory.Options bfo=new BitmapFactory.Options();
if (!f.exists() || !f.isDirectory())
{
return songs;
}
File[] files = f.listFiles(new Mp3Filter());
for(int i=0; i<files.length; i++)
{
if (files[i].isFile())
{
//mmr.setDataSource(files[i].getPath());
// rawArt = mmr.getEmbeddedPicture();
SongDetails detail=new SongDetails();
//if ( rawArt != null)
//{
//bitmap2=BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
//bitmap2 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);
//detail.setIcon(bitmap2);
//}//else
//{
detail.setIcon(bitmap2);
//}
detail.setSong(files[i].getName());
// detail.setArtist(files[i].getName());
// detail.setAlbum(files[i].getName());
//detail.setArtist(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
// detail.setAlbum(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
detail.setPath2( files[i].getPath()) ;
songs.add(detail);
}
else if (files[i].isDirectory())
{
songs.addAll(getSongsFromDirectory(files[i]));
}
} return songs;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
info = (AdapterContextMenuInfo) menuInfo;
menu.add(Menu.NONE, v.getId(), 0, "Play");
menu.add(Menu.NONE, v.getId(), 0, "Delete");
menu.add(Menu.NONE, v.getId(), 0, "Queue Item");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Play") {
}
else if (item.getTitle() == "Delete") {
}
else if (item.getTitle() == "Queue Item") {
}
else {
return false;
}
return true;
}}
class Mp3Filter implements FileFilter
{
public boolean accept(File file)
{
return (file.isDirectory()||file.getName().endsWith(".apk")|| file.getName().endsWith(".Mp3"));
}
}
The Log
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = sourcecode.jazzplayer.SongDetails)
java.io.NotSerializableException:
Is your classSonginfo2
serializable? – Lewanna