2

我正在創建在線媒體播放器。用戶可以創建自己的播放列表。 因此,每個用戶都有不同的列表。並且在該列表中有一個名爲Quickmix的項目已修復,該項目位於該列表的頂部。 我想在Quickmix文本附近顯示圖標,並且還想顯示當前正在播放的播放列表名稱附近的圖標。 用於顯示該列表我在xml文件中創建了一個ListView。 我創建了PlayListActivity.java,它擴展了Activity。 在I類已創建了一個ArrayAdapter如何在android中顯示ArrayAdapter的文本和圖標列表?

private ArrayAdapter<String> mPlayListAdapter = null; 
private ListView mPlayList = null; 
private String[] mPlayListNames = null; 

和在onCreate方法我的代碼是這樣

setContentView(R.layout.playlist_layout); 

      mPlayList = new String[array.length ]; 
     for(int i = 0; i < length; i++) 
     { 
      mPlayListNames[i] = array[i].mPlayList; 
     } 
    mPlayList = (ListView)findViewById(R.id.playList); 
    try { 
     mPlayListAdapter = 
      new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       mPlayListNames); 
     mPlayList.setAdapter(mPlayListAdapter); 
     mPlayList.setOnItemClickListener(mPlayListListener); 
    } catch(Exception e) { 
     Intent nextActivityIntent = new Intent(
      getApplicationContext(), 
      Welcome.class); 
     finish(); 
     startActivity(nextActivityIntent); 
     return; 
    } 
/** 
* Called when user clicks on any playlist name. This listener starts 
* playback of that playlist. 
*/ 
private OnItemClickListener mPlayListListener = 
    new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) 
     { 
      Intent nextActivityIntent = new Intent(view.getContext(), 
       SomeActivity.class); 
      //Some code to start SomeActivity 
     } 
    }; 

我不能延伸ListActivity /任何給PlayListActivity.java 我只能延伸活性的影響。

有沒有解決方法如何做到這一點? I want o/p like this

回答

0

您必須創建ListAdapter。例如:

private class EfficientAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 
    private Bitmap mIcon1; 
    private Bitmap mIcon2; 

    public EfficientAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 


    } 

    /** 
    * The number of items in the list is determined by the number of speeches 
    * in our array. 
    * 
    * @see android.widget.ListAdapter#getCount() 
    */ 
    public int getCount() { 

     return stations.length; 
    } 

    /** 
    * Since the data comes from an array, just returning the index is 
    * sufficent to get at the data. If we were using a more complex data 
    * structure, we would return whatever object represents one row in the 
    * list. 
    * 
    * @see android.widget.ListAdapter#getItem(int) 
    */ 
    public Object getItem(int position) { 
     return position; 
    } 

    /** 
    * Use the array index as a unique id. 
    * 
    * @see android.widget.ListAdapter#getItemId(int) 
    */ 
    public long getItemId(int position) { 
     return position; 
    } 

    /** 
    * Make a view to hold each row. 
    * 
    * @see android.widget.ListAdapter#getView(int, android.view.View, 
    *  android.view.ViewGroup) 
    */ 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // A ViewHolder keeps references to children views to avoid unneccessary calls 
     // to findViewById() on each row. 

     // When convertView is not null, we can reuse it directly, there is no need 
     // to reinflate it. We only inflate a new View when the convertView supplied 
     // by ListView is null. 
     if (convertView == null) { 

      convertView = mInflater.inflate(R.layout.listitem, null); 

      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 

      TextView textView = (TextView) convertView.findViewById(R.id.text1); 
      textView.setText(stations[position]); 


     } else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 

     } 


     return convertView; 
    } 



} 

此適配器,您可以使用這樣的:

mPlayList.setListAdapter(new EfficientAdapter(ctx)); 

你怎麼樣理解,在R.layout.listitem您可以創建按鈕。在方法getView中 - 你必須處理這個按鈕。

UPD:爲例使用位圖: 可以在getView補充一點:

ImageView im = new ImageView(this); 
im.setImageBitmap(bitmap); 
convertView.addView(im); 
+0

我應該取代mPlayList.setAdapter(mPlayListAdapter); with mPlayList.setListAdapter(new EfficientAdapter(getApplicationContext())); – shankey 2011-12-17 11:53:32

+0

是的。並在EfficientAdapter中,您可以創建任意數量的按鈕 – Anton 2011-12-17 11:54:30

+0

Button - 這是一個例子。您可以創建任何視圖。 – Anton 2011-12-17 12:02:15

相關問題