【问题标题】:How to correctly display a list of songs using an ArrayAdapter?如何使用 ArrayAdapter 正确显示歌曲列表?
【发布时间】:2010-04-20 04:09:03
【问题描述】:

我正在尝试使用数组适配器显示歌曲列表。但问题是我无法显示列表,并且只显示具有预设背景的空屏幕。这是代码...所有这些都是单独的类...请帮助我...

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

}   

}

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    Arrayadapter 需要一个项目列表才能正常工作。 目前,您正在 getView 方法中创建一个空列表,然后尝试从中请求一个项目。你只能在不破坏你的程序的情况下这样做,因为这个函数永远不会被调用。

    在显示 ListView 的那一刻,ListView 将询问底层适配器适配器中有多少项。 ArrayListAdapter 将返回定义此适配器的内部列表的大小。您没有在超级构造函数中向 ArrayAdapter 提供这样的列表,因此您的 Adapter 从一个空列表开始,如果您不向其中添加某些内容,您的 Adapter 在 ListActivity 的整个运行时将保持为空。

    解决这个问题的最简单方法是在创建适配器之前创建所有 SongLists 的列表(顺便说一下,这不是最好的名称选择,因为对象不是列表)在创建适配器之前显示,然后将它们添加到 ArrayAdapter通过适配器的构造函数中的super constructor。现在适配器可以正常工作并调用您的 getView 方法。

    【讨论】:

    • 我无法获得您要求我遵循的程序---"在创建适配器之前创建所有 SongLists 的列表以显示,然后通过构造函数中的超级构造函数将它们添加到 ArrayAdapter你的适配器。”你能解释一下或提供一些关于如何做的示例代码吗???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多