【发布时间】:2013-03-08 00:45:42
【问题描述】:
我想创建一个ListView 适配器来在一行中显示多个数据,例如Username、Comment、Date 和Rating。这就像在论坛中显示 cmets。我有 4 个数组,每个数组都存储其中一个值(Username、Comment、Date 和 Rating)。我尝试使用以下代码为此创建自定义适配器。但我真的不知道实现这一点的正确方法。我尝试运行它,它只显示了Username 和Date。我不确定这段代码是否是正确的实现方式。
Float f = Float.parseFloat(commentRating[position]);
holder.cmtRating.setRating(f);
Content.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;
}
}
感谢任何答案和cmets。
编辑
comment.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 安排出现了某种问题。一旦我重新安排它,评论就会出现,一切正常。谢谢大家的时间。
【问题讨论】:
-
你的xml怎么样?你有所有的东西吗?
-
您的适配器看起来不错,请发布
comment.xml,以便我们查看问题是否出在布局中。commentText[position]是否返回非空字符串? -
这不可能是你所有的 xml
-
您是否将虚拟数据放入您的 xml 以查看它是否显示在图形视图中?
-
@codeMagic 对不起。那不是我的整个 xml。我也在问题中包含了另一个 xml。是的。我确实尝试在我的 xml 中输入一些虚拟数据。一切都好
标签: android android-listview listadapter android-adapter