Android 在适配器中使用getViewById()
我有一个扩展BaseAdapter的类,对于Android 在适配器中使用getViewById(),android,xml,list,view,adapter,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,
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();
}
}
公共类FeedAdapter扩展了BaseAdapter
{
私人语境;
私有ArrayList提要;
私有字符串状态;
公共FeedAdapter(上下文上下文、ArrayList数据、字符串st)
{
超级();
this.context=上下文;
feed=数据;
state=st;
}
/**
*实施方法
*/
公共视图getView(内部位置、视图行、视图组列表)
{
//行对象!
if(行==null){
RelativeLayout newRow=新的RelativeLayout(上下文);
}
否则{
返回行;
}
}
@凌驾
public int getViewTypeCount()
{
//有两种类型的视图图像和文本。
//但现在将其设置为1。
返回1;
}
公共长getItemId(int位置)
{
//同步
返回位置;
}
公共对象getItem(int位置)
{
返回进给。获取(位置);
}
public int getCount()
{
返回feed.size();
}
}
我的问题是,我不能使用getViewById()
,除非我有“行布局”
”的父视图,但如果我不能得到它,那就有点像第22条军规
那么我该怎么办呢?您可以使用
行.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>
row.xml中层次结构顶部的视图是一个RelativeLayout标记。你能上传布局文件吗?好的,我这么做了。它不是RelativeLayout rl=new RelativeLayout(上下文),而是RelativeLayout rl=(RelativeLayout)row.findViewById(R.id.RelativeLayout)如果一行是空指针,则意味着您可能正在插入一组空的dataJois u dumbass,这是循环使用之前的第一个行实例。