在Adapter中使用getViewById()

时间:2016-02-19 00:40:53

标签: android xml list view adapter

我有一个扩展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();
}
}

我的问题是,我不能使用getViewById(),除非我有'row-layout'的父视图,但如果我甚至无法得到它,那就是Catch-22的情况。

那我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以使用row.findViewById(R.id.etc),因为row是一个View,您可以使用此功能。三江源

答案 1 :(得分:0)

<?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>