2010-04-20 71 views
0

我想顯示使用陣列適配器的歌曲列表。但問題是我不能顯示列表,只有預設背景的空白屏幕顯示出來。下面的代碼......所有你是單獨的類... plz幫助我...如何使用ArrayAdapter正確顯示歌曲列表?

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

} 

} 

回答

0

一個ArrayAdapter需要項目列表正常工作。 目前,您正在getView方法中創建一個空列表,然後嘗試從中請求一個項目。你只能做到這一點,而不會打亂你的程序,因爲這個功能永遠不會被調用。

在顯示ListView的那一刻,ListView將詢問底層適配器中適配器的數量。 ArrayListAdapter將返回定義此適配器的內部列表的大小。您不會在超級構造函數中爲您的ArrayAdapter提供這樣的列表,因此您的適配器以空列表開始,並且如果您不向其添加任何內容,則適配器將保留爲ListActivity的整個運行時爲空。

解決此問題的最簡單方法是在創建適配器之前創建所有SongLists的列表(順便說一下,不是名稱的最佳選擇,因爲該對象不是列表),然後將其添加到您的適配器通過適配器構造函數中的super constructor ArrayAdapter。現在適配器可以正常工作並調用您的getView方法。

+0

我無法獲得您要求我遵循的程序---「在創建適配器之前創建要顯示的所有SongLists的列表,然後通過適配器構造函數中的超級構造函數將它們添加到ArrayAdapter中。你可以解釋或提供一些關於如何做的示例代碼? – 2010-04-20 11:31:21