如何正确显示的使用ArrayAdapter的歌曲列表? [英] How to correctly display a list of songs using an ArrayAdapter?

查看:92
本文介绍了如何正确显示的使用ArrayAdapter的歌曲列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示的使用数组适配器歌曲列表。但问题是,我不能显示列表,只有空的屏幕,preSET背景显示出来。这里的code ...所有你是单独的类... plz帮助我...

 公共类SongsAdapter扩展ArrayAdapter< SongsList> {
私人上下文的背景下;
TextView的tvTitle;
TextView的tvMovie;
TextView的tvSinger;
字符串s;
    公共SongsAdapter(上下文的背景下,INT资源,
            INT textViewResourceId,字符串名称){
        超级(上下文,资源,textViewResourceId);
        this.context =背景;
    }
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    最终诠释我=位置;    清单< SongsList> listSongs =新的ArrayList< SongsList>();
    字符串title = listSongs.get(I).gettitleName()的toString()。    字符串专辑= listSongs.get(I).getmovieName()的toString()。
    串艺术家= listSongs.get(ⅰ).getsingerName()的toString();
    。字符串imgal = listSongs.get(I).gettitleName()的toString();    LayoutInflater充气=((活动)上下文).getLayoutInflater();
    视图V = inflater.inflate(R.layout.row,NULL);
    tvTitle =(TextView中)v.findViewById(R.id.text2);
    tvMovie =(TextView中)v.findViewById(R.id.text3);
    tvSinger =(TextView中)v.findViewById(R.id.text1);
    tvTitle.setText(职称);
    tvMovie.setText(册页);
    tvSinger.setText(艺术家);    最后ImageView的IM =(ImageView的)v.findViewById(R.id.image);
        S =htt​​p://www.gorinka.com/+ imgal;
         串imgPath =秒;
        AsyncImageLoaderv asyncImageLoaderv =新AsyncImageLoaderv();
        位图cachedImage = asyncImageLoaderv.loadDrawable(imgPath,新AsyncImageLoaderv.ImageCallback(){
            公共无效imageLoaded(位图imageDrawable,串图片网址){                im.setImageBitmap(imageDrawable);
           }
          });
         im.setImageBitmap(cachedImage);       返回伏;}


 公共类ImageLoader的实现Runnable {        私人字符串SS;
        私人ImageView的IM;         公共ImageLoader的(一个String,ImageView的IM){
             this.ss =秒;
             this.im = IM;
             线程线程=新主题(本);
             thread.start();
            }
        公共无效的run(){
             尝试{                 HTTPGET HTT prequest = NULL;
                  HTT prequest =新HTTPGET(SS);
                  HttpClient的HttpClient的=新DefaultHttpClient();
                    HTT presponse响应=(HTT presponse)httpclient.execute(HTT prequest);
                    HttpEntity实体= response.getEntity();
                    BufferedHttpEntity bufHttpEntity =新BufferedHttpEntity(实体);
                     InputStream为= bufHttpEntity.getContent();                     位图BM = BitmapFactory.de codeStream(是);
                     Log.d(IMG,IMG);                     is.close();
                     im.setImageBitmap(BM);             }赶上(例外T){
                Log.e(位图URL,异常在updateStatus(),T);            }     }
 }
}


 公共类SongsList {私人字符串TITLENAME;
私人字符串的movieName;
私人字符串singerName;
私人字符串的ImagePath;
私人字符串mediaPath;
//构造函数SongsList类
公共SongsList(字符串TITLENAME,字符串的movieName,字符串singerName,字符串的ImagePath,字符串mediaPath){
    超();
    this.titleName = TITLENAME;
    this.movi​​eName =的movieName;
    this.singerName = singerName;
    this.imagePath =的ImagePath;
    this.mediaPath = mediaPath;
}
公共字符串gettitleName()
{
    返回TITLENAME;
}
公共无效settitleName(字符串TITLENAME){
    this.titleName = TITLENAME;
}
公共字符串getmovieName()
{
    返回的movieName;
}
公共无效setmovieName(字符串的movieName){
    this.movi​​eName =的movieName;
}
公共字符串getsingerName()
{
    返回singerName;
}
公共无效setsingerName(字符串singerName){
    this.singerName = singerName;
}
公共字符串getimagePath()
{
    返回的ImagePath;
}
公共无效setimagePath(字符串的ImagePath){
    this.imagePath =的ImagePath;
}
公共字符串getmediaPath()
{
    返回mediaPath;
}
公共无效setmediaPath(字符串mediaPath){
    this.mediaPath = mediaPath;
}
}


 公共类MusicListActivity延伸活动{@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.openadiuofile);    ListView控件列表=(ListView控件)findViewById(R.id.list1);
    SongsAdapter适配器=新SongsAdapter(这一点,R.layout.row,R.id.text2,NULL);
    list.setAdapter(适配器);}}


解决方案

一个ArrayAdapter需要的项目列表才能正常工作。
此刻你在你的getView方法创建一个空的列表,然后试图请求一个项目出来。你只能做,没有chrashing你的程序,因为该功能不会被调用。

目前一个ListView显示在ListView会问底层适配器许多项目是如何在适配器。该ArrayListAdapter将返回内部列表定义该适配器的大小。你没有在超级构造函数提供这样一份名单,以你的ArrayAdapter为此适配器与空List开始,如果你不添加东西,它的适配器将保持为空的ListActivity的整个运行时间。

解决这个最简单的方法是创建所有SongLists的名单(顺便说一句没有名字的最佳选择becaue thechnically对象是不是一个清单)创建适配器之前显示,然后将它们添加到您的ArrayAdapter通过<一个href=\"http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20java.util.List%3CT%3E%29\"相对=nofollow>在适配器的构造超级构造。现在,适配器可以正常工作,并致电getView方法。

I am trying to display the list of songs using array adapters. But the problem is i couldnt display the list and only empty screen with preset background is showing up. Here's the code...All the thee are seperate classes... Plz help me...

public class SongsAdapter extends ArrayAdapter<SongsList>{
private Context context;
TextView tvTitle;
TextView tvMovie;
TextView tvSinger;
String s;


    public SongsAdapter(Context context, int resource,
            int textViewResourceId, String title) {
        super(context, resource, textViewResourceId);
        this.context=context;
    }


public View getView(int position, View convertView, ViewGroup parent) {
    final int i=position;   

    List<SongsList> listSongs = new ArrayList<SongsList>();
    String title = listSongs.get(i).gettitleName().toString();

    String album = listSongs.get(i).getmovieName().toString();
    String artist = listSongs.get(i).getsingerName().toString();
    String imgal = listSongs.get(i).gettitleName().toString();

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    View v = inflater.inflate(R.layout.row, null); 
    tvTitle=(TextView)v.findViewById(R.id.text2);
    tvMovie=(TextView)v.findViewById(R.id.text3);
    tvSinger=(TextView)v.findViewById(R.id.text1);
    tvTitle.setText(title);
    tvMovie.setText(album);
    tvSinger.setText(artist);

    final ImageView im=(ImageView)v.findViewById(R.id.image);
        s="http://www.gorinka.com/"+imgal;
         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 ImageView im;

         public imageloader(String s,  ImageView im) {
             this.ss=s;
             this.im=im;
             Thread thread = new Thread(this);
             thread.start(); 
            }
        public void run(){
             try {

                 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();

                     Bitmap bm = BitmapFactory.decodeStream(is);
                     Log.d("img","img");

                     is.close();
                     im.setImageBitmap(bm);

             } catch (Exception t) {
                Log.e("bitmap url", "Exception in updateStatus()", t);

            }

     } 
 }
}


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 MusicListActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.openadiuofile);

    ListView list = (ListView)findViewById(R.id.list1);
    SongsAdapter adapter = new SongsAdapter(this,R.layout.row, R.id.text2, null);
    list.setAdapter(adapter);

}   

}

解决方案

The Arrayadapter needs a list of items to work properly. At the moment you are creating an empty list in your getView method and then trying to request an item out of it. You can only do that without chrashing your program because this function is never called.

At the moment a ListView is shown the ListView will ask the underlying adapter how many items are in the adapter. The ArrayListAdapter will return the size of the internal list defining this adapter. You don't supply such a list to your ArrayAdapter in the super constructor therefor your Adapter starts with an empty list and if you don't add something to it your Adapter will stay empty for the whole runtime of the ListActivity.

The easiest way to solve this is to create the List of all SongLists (By the way not the best choice of name becaue thechnically the object isn't a list) to display before creating your adapter and then add them to your ArrayAdapter via the super constructor in the Constructor of your Adapter. Now the Adapter can work properly and call your getView method.

这篇关于如何正确显示的使用ArrayAdapter的歌曲列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆