Displaying music list using custom lists instead of array adapters

Posted by Rahul Varma on Stack Overflow See other posts from Stack Overflow or by Rahul Varma
Published on 2010-04-19T05:01:09Z Indexed on 2010/04/19 5:03 UTC
Read the original article Hit count: 349

Filed under:

Hi,

I have displayed the music list in a list view. The list is obtained from a website. I have done this using Arraylist. Now, i want to iterate the same program using custom lists and custom adapters instead of array list. The code i have written using array lists is...

public class MusicListActivity extends Activity {
MediaPlayer mp;
File  mediaFile;
TextView tv;
TextView albumtext;
TextView artisttext;
ArrayList<String> al=new ArrayList<String>();
//ArrayList<String> al=new ArrayList<String>();
ArrayList<String> node=new ArrayList<String>();
ArrayList<String> filepath=new ArrayList<String>();
ArrayList<String> imgal=new ArrayList<String>();
ArrayList<String> album=new ArrayList<String>(); 
ArrayList<String> artist=new ArrayList<String>();
ListView lv;
 Object[] webImgListObject;
 String[] stringArray;
 XMLRPCClient client;
 String loginsess;
 HashMap<?, ?> siteConn = null;
 //ImageView im;
 Bitmap img;
 String s;
 int d;
 int j;
 StreamingMediaPlayer sm;
 int start=0;
 Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.openadiuofile);
    lv=(ListView)findViewById(R.id.list1);
    al=getIntent().getStringArrayListExtra("titles");
    //node=getIntent().getStringArrayListExtra("nodeid");
    filepath=getIntent().getStringArrayListExtra("apath");
    imgal=getIntent().getStringArrayListExtra("imgpath");
    album=getIntent().getStringArrayListExtra("album");
    artist=getIntent().getStringArrayListExtra("artist");
    // ArrayAdapter<String> aa=new ArrayAdapter<String>(this,R.layout.row,R.id.text2,al);
     //lv.setAdapter(aa);
    try{

       lv.setAdapter( new styleadapter(this,R.layout.row, R.id.text2,al)); 

    }catch(Throwable e)
    {
        Log.e("openaudio error",""+e.toString());
        goBlooey(e);
    }
       lv.setOnItemClickListener(new OnItemClickListener(){      
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3){ j=1;
             try{  d=arg2;
               String filep=filepath.get(d);
               String tit=al.get(d);
               String image=imgal.get(d);   
               String singer=artist.get(d);
               String movie=album.get(d);

              sendpath(filep,tit,image,singer,movie);
             // getpath(n);             
        }catch(Throwable t)
        {
            goBlooey(t);
        }
        }

       });

   } 
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if(j==0)         
    {i=new Intent(this,gorinkadashboard.class);
    startActivity(i);}
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    j=0;
}
 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_SEARCH)
            {
            Log.i("go","go");

            return true;        
        }

        return(super.onKeyDown(keyCode, event));
    }

 public void sendpath(String n,String nn,String image,String singer,String movie)
 {     

        Intent ii=new Intent(this,MusicPlayerActivity.class);
        ii.putExtra("path",n);
        ii.putExtra("titletxt",nn);
        //ii.putStringArrayListExtra("playpath",filepath);
        ii.putExtra("pos",d);
        ii.putExtra("image",image);
        ii.putStringArrayListExtra("imagepath",imgal);
        ii.putStringArrayListExtra("filepath", filepath);
        ii.putStringArrayListExtra("imgal", imgal);
        ii.putExtra("movie" ,movie );
        ii.putExtra("singer",singer);
        ii.putStringArrayListExtra("album", album);
        ii.putStringArrayListExtra("artist",artist);
        ii.putStringArrayListExtra("tittlearray",al);
        startActivity(ii);
 }


    class styleadapter extends ArrayAdapter<String>
    {
       Context context=null;
        public styleadapter(Context context, int resource,
                int textViewResourceId, List<String> objects) {
            super(context, resource, textViewResourceId, objects);

            this.context=context;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final int i=position;           
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();                     
            View v = inflater.inflate(R.layout.row, null); 
            tv=(TextView)v.findViewById(R.id.text2);
            albumtext=(TextView)v.findViewById(R.id.text3);
            artisttext=(TextView)v.findViewById(R.id.text1);
            tv.setText(al.get(i));
           albumtext.setText(album.get(i));
            artisttext.setText(artist.get(i));
            final ImageView im=(ImageView)v.findViewById(R.id.image);
                s="http://www.gorinka.com/"+imgal.get(i);
           //   displyimg(s,v);    
               // new imageloader(s,im);
              String imgPath=s;
             AsyncImageLoaderv asyncImageLoaderv=new AsyncImageLoaderv();
            Bitmap cachedImage = asyncImageLoaderv.loadDrawable(imgPath, new AsyncImageLoaderv.ImageCallback() {
                    public void imageLoaded(Bitmap imageDrawable, String imageUrl) {

                        im.setImageBitmap(imageDrawable);
                   }
                  });
                 im.setImageBitmap(cachedImage);

               return v;

        }    

        }


    public class imageloader implements Runnable{

        private String ss;
        //private View v;
        //private View v2;
        private ImageView im;

         public imageloader(String s,  ImageView im) {
             this.ss=s;
            //this.v2=v2;
             this.im=im;
             Thread thread = new Thread(this);
             thread.start(); 
            }
        public void run(){
             try {
                    // URL url = new URL(ss);
                    // URLConnection conn = url.openConnection();
                    // conn.connect();
                 HttpGet httpRequest = null;            
                  httpRequest = new HttpGet(ss);
                  HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                     InputStream is = bufHttpEntity.getContent();
                   //  BufferedInputStream bis = new BufferedInputStream(is);
                     Bitmap bm = BitmapFactory.decodeStream(is);
                     Log.d("img","img");
                    // bis.close();
                     is.close();
                     im.setImageBitmap(bm);
                    // im.forceLayout();
                    // v2.postInvalidate();
                   //  v2.requestLayout(); 
             } catch (Exception t) {
                Log.e("bitmap url", "Exception in updateStatus()", t);
                //goBlooey(t);
                // throw new RuntimeException(t);
            }

     } 


    }

    private void goBlooey(Throwable t) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);

        builder
            .setTitle("Exception!")
            .setMessage(t.toString())
            .setPositiveButton("OK", null)
            .show();
    }

  }

I have created the SongList.java, SongsAdapter.java and also SongsAdapterView.java. Their code is...

public class SongsList {

private String titleName;
private String movieName;
private String singerName;
private String imagePath;
private String mediaPath;

// Constructor for the SongsList class public SongsList(String titleName, String movieName, String singerName,String imagePath,String mediaPath ) { super(); this.titleName = titleName; this.movieName = movieName; this.singerName = singerName; this.imagePath = imagePath; this.mediaPath = mediaPath; }

public String gettitleName() 
{
    return titleName;
}
public void settitleName(String titleName) {
    this.titleName = titleName;
}
public String getmovieName() 
{
    return movieName;
}
public void setmovieName(String movieName) {
    this.movieName = movieName;
}
public String getsingerName() 
{
    return singerName;
}
public void setsingerName(String singerName) {
    this.singerName = singerName;
}
public String getimagePath() 
{
    return imagePath;
}
public void setimagePath(String imagePath) {
    this.imagePath = imagePath;
}
public String getmediaPath() 
{
    return mediaPath;
}
public void setmediaPath(String mediaPath) {
    this.mediaPath = mediaPath;
}

}

public class SongsAdapter extends BaseAdapter{
private Context context;
private List<SongsList> listSongs;

public SongsAdapter(Context context, List<SongsList> listPhonebook){
    this.context = context;
    this.listSongs = listSongs;
    }
public int getCount() {
    return listSongs.size();
}

public Object getItem(int position) {
    return listSongs.get(position);
}

public long getItemId(int position) {
    return position;
}
public View getView(int position, View view, ViewGroup viewGroup) {
    SongsList entry = listSongs.get(position);
    return new SongsAdapterView(context,entry);
}

}

public SongsAdapterView(Context context, SongsList entry) {
    super(context);
    this.setOrientation(VERTICAL);
    this.setTag(entry);
    // TODO Auto-generated constructor stub

    View v = inflate(context, R.layout.row, null);

    TextView tvTitle = (TextView)v.findViewById(R.id.text2);
    tvTitle.setText(entry.gettitleName());

    TextView tvMovie = (TextView)v.findViewById(R.id.text3);
    tvTitle.setText(entry.getmovieName());

    TextView tvSinger = (TextView)v.findViewById(R.id.text1);
    tvTitle.setText(entry.getsingerName());

    addView(v);


}

}

Can anyone please tell me how to display the list using custom lists and custom adapters using the code above???

© Stack Overflow or respective owner

Related posts about android