我想创建一个ListView
适配器来显示多个数据,例如Username
, Comment
,Date
和Rating
一起显示在一行中。这就像在论坛中显示评论一样。我有 4 个数组,每个数组都存储其中一个值(、Username
和Comment
)。我尝试使用以下代码为此创建自定义适配器。但我真的不知道实现这一点的正确方法。我尝试运行它,它只显示和. 我不确定这段代码是否是正确的实现方式。Date
Rating
Username
Date
Float f = Float.parseFloat(commentRating[position]);
holder.cmtRating.setRating(f);
内容.java:
list = (ListView) findViewById(R.id.commentlist);
adapter=new CommentAdapter(ContentScreen.this);
list.setAdapter(adapter);
public class CommentAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater=null;
public CommentAdapter(Activity a) {
activity = a;
inflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return commentText.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public class ViewHolder{
public TextView cmtUser;
public TextView cmtText;
public RatingBar cmtRating;
public TextView cmtTime;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.comment, null);
holder = new ViewHolder();
holder.cmtUser = (TextView) vi.findViewById(R.id.commentby);
holder.cmtText = (TextView) vi.findViewById(R.id.comment);
holder.cmtRating = (RatingBar) vi.findViewById(R.id.commentrating);
holder.cmtTime = (TextView) vi.findViewById(R.id.commentcreatedby);
vi.setTag(holder);
} else
holder=(ViewHolder)vi.getTag();
holder.cmtUser.setText(commentBy[position]);
holder.cmtText.setText(commentText[position]);
Float f = Float.parseFloat(commentRating[position]);
holder.cmtRating.setRating(f);
holder.cmtTime.setText(commentCreatedTime[position]);
return vi;
}
}
任何答案和意见表示赞赏。
编辑
评论.xml
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/commentcreatedby"
android:layout_alignParentLeft="true"
android:layout_below="@+id/commentby"
android:paddingLeft="5dp"
android:paddingTop="5dp"/>
<TextView
android:id="@+id/commentcreatedby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingRight="5dp"
android:paddingTop="5dp"/>
<TextView
android:id="@+id/commentby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:paddingTop="5dp" />
<RatingBar
android:id="@+id/commentrating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/comment"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp" />
内容.xml
<include
android:id="@+id/top_header"
android:layout_width="match_parent"
layout="@layout/header" />
<TextView
android:id="@+id/content_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/top_header"
android:padding="10dip"/>
<ImageView
android:id="@+id/content_image"
android:layout_width="fill_parent"
android:layout_height="160dip"
android:layout_alignParentLeft="true"
android:layout_below="@+id/content_desc"
android:padding="10dip" />
<ListView
android:id="@+id/commentlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/content_price" >
</ListView>
<TextView
android:id="@+id/content_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/content_image"
android:layout_marginLeft="29dp"
android:layout_marginTop="44dp"
android:padding="10dip"
android:textSize="20dp" />
<Button
android:id="@+id/content_subscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/commentlist"
android:layout_alignParentRight="true"
android:layout_marginRight="38dp"
android:padding="20dip"
android:paddingRight="20dip"
android:text="Subscribe" />
更新
评级也有效。但它与日期叠加在一起。至于评论,我仍然无法正常工作。
更新(已解决) 显然这是我的comment.xml 安排出现了某种问题。一旦我重新安排它,评论就会出现,一切正常。谢谢大家的时间。