Я хотел создать ListView
адаптер для отображения нескольких данных, таких как Username
, Comment
, Date
и Rating
вместе в одном ряду. Это как отображение комментариев на форумах. У меня есть 4 массива, и каждый из массивов хранит одно из этих значений (, Comment
, Date
и Rating
). Я попытался использовать приведенный ниже код для создания пользовательского адаптера для этого. Но я не знаю, как правильно это реализовать. Я попытался запустить это, и он отобразил только Username
и Date
. Я не уверен, является ли этот код правильным способом реализации или нет.ListView адаптер
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;
}
}
Любые ответы и комментарии приветствуются.
РЕДАКТИРОВАТЬ
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" />
content.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" />
UPDATE
Оценка работает, как хорошо. Но он складывается вместе с датой. Что касается комментария, я все еще не могу заставить его работать.
UPDATE (SOLVED) По-видимому, это мое соглашение comment.xml получило какую-то проблему. После того, как я его перестрою, появится комментарий, и все работает нормально. Спасибо всем за время.
Как ваш xml? У вас есть все, что там показано? – codeMagic
Ваш Адаптер выглядит хорошо, напишите 'comment.xml', чтобы мы могли видеть, есть ли проблема в макете.Does 'commentText [position]' возвращает непустую строку? – Sam
Это не все ваши xml – codeMagic