2014-01-21 18 views
0

我有一個問題,目前我正在使用CursorTreeAdapter但是我想改變它使用任何其他數據不只是遊標,但說實話我不知道我該怎麼做,我認爲實施一些其他適配器然後重寫所需的方法?但是,有沒有人能以某種方式向我展示?我現在很困惑,不知道我該走什麼方向。 感謝您的幫助。自定義適配器執行

回答

0

你可以看到這個例子:

public class FeedAdapter extends BaseAdapter { 
    private ArrayList<FeedItem> items; 
    private LayoutInflater layoutInflater; 

    FeedAdapter(Context context, ArrayList<FeedItem> list, int bgColor){ 
     items = list; 
     layoutInflater = LayoutInflater.from(context); 
    } 


    @Override 
    public int getCount() { 
     return items.size(); 
    } 


    @Override 
    public FeedItem getItem(int index) { 
     return items.get(index); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row; 

     if (null == convertView) { 
        row = layoutInflater.inflate(R.layout.romanblack_feed_item, null); 
     } else { 
      row = convertView; 
     } 

     TextView title = (TextView) row.findViewById(R.id.romanblack_rss_title); 
     TextView pubdate = (TextView) row.findViewById(R.id.romanblack_rss_pubdate); 

       String titleString = items.get(position).getTitle(); 
       title.setText(titleString); 

       if(items.get(position).getTextColor() != Color.TRANSPARENT){ 
      title.setTextColor(items.get(position).getTextColor()); 
       }else{ 
        title.setTextColor(Color.DKGRAY); 
       } 

     pubdate.setText(items.get(position).getPubdate()); 

     return row; 
    } 

} 

和佈局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/romanblack_feed_item" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#FFF"> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/romanblack_rss_bg_feed" 
     android:orientation="vertical"> 
     <TextView 
      android:text="Title" 
      android:id="@+id/romanblack_rss_title" 
      android:textSize="14sp"    
      android:textColor="#222" 
      android:layout_margin="5dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <TextView 
      android:text="2011-01-01" 
      android:id="@+id/romanblack_rss_pubdate" 
      android:textSize="10sp"    
      android:textColor="#666" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</LinearLayout> 
+0

感謝您對我的例子。 – user2141889