Android 自定义适配器实现
我有一个问题,目前我正在使用CursorTreeAdapter,但我想将其更改为使用任何其他数据,不仅仅是光标,但老实说,我不知道我该如何做,我想实现一些其他适配器,然后覆盖所需的方法?但是有谁能给我指点路吗?我现在很困惑,不知道该往哪个方向走。Android 自定义适配器实现,android,adapter,Android,Adapter,我有一个问题,目前我正在使用CursorTreeAdapter,但我想将其更改为使用任何其他数据,不仅仅是光标,但老实说,我不知道我该如何做,我想实现一些其他适配器,然后覆盖所需的方法?但是有谁能给我指点路吗?我现在很困惑,不知道该往哪个方向走。 谢谢您的帮助。您可以扩展的抽象类:。您可以看到以下示例: public class FeedAdapter extends BaseAdapter { private ArrayList<FeedItem> items; p
谢谢您的帮助。您可以扩展的抽象类:。您可以看到以下示例:
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;
}
}
公共类FeedAdapter扩展了BaseAdapter{
私有ArrayList项;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
FeedAdapter(上下文、ArrayList列表、int-bgColor){
项目=列表;
layoutInflater=layoutInflater.from(上下文);
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共FeedItem getItem(整数索引){
返回项目。获取(索引);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
查看行;
if(null==convertView){
行=布局平坦。充气(R.layout.romanblack\u feed\u item,空);
}否则{
行=转换视图;
}
TextView title=(TextView)row.findViewById(R.id.romanblack\u rss\u title);
TextView pubdate=(TextView)row.findviewbyd(R.id.romanblack\u rss\u pubdate);
String titleString=items.get(position.getTitle();
title.setText(标题字符串);
if(items.get(position.getTextColor()!=Color.TRANSPARENT){
title.setTextColor(items.get(position.getTextColor());
}否则{
title.setTextColor(Color.DKGRAY);
}
setText(items.get(position.getPubdate());
返回行;
}
}
和布局文件
<?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>