在适配器中使用 getViewById()

using getViewById() in Adapter

我有一个 class 扩展 BaseAdapter,对于 getView() 实现,我想调用一个预先存在的行模板。

public class FeedAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<ArrayList<String>> feed;
    private String state;

    public FeedAdapter(Context context, ArrayList<ArrayList<String>> data, String st)
    {
        super();

        this.context = context;
        feed = data;
        state = st;
    }


/**
 * IMPLEMENTED METHODS
 */
public View getView(int position, View row, ViewGroup list)
{
    //the row OBJECT!
    if (row == null){
        RelativeLayout newRow = new RelativeLayout(context);

    }
    else {
        return row;
    }
}

@Override
public int getViewTypeCount()
{
   //There are 2-types of View image and text.
    //but set it as 1 for now.
    return 1;
}

public long getItemId(int position)
{
    //SYNCHRONISED
    return position;
}

public Object getItem(int position)
{
    return feed.get(position);
}

public int getCount()
{
    return feed.size();
}
}

我的问题是,除非我有“row-layout”的父视图,否则我无法使用 getViewById(),但如果我什至无法获得,这就是第 22 条军规那。

那我该怎么办?

您可以使用 row.findViewById(R.id.etc) ,因为行是一个视图,您可以使用此功能。谢谢

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<!--FOR IMAGE lookup ImageView-API for how-to input image-->
<ImageView
    android:id="@+id/picture"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription=""
    />

<!--STARTING WITH title-->
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/picture"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text=""
    android:textSize="20sp"
    />

<TextView
    android:id="@+id/body"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true" />
</RelativeLayout>