我正在显示评论列表。我能够从服务器获取列表。但它不会显示在RecyclerView中。在logcat中,它显示如下:
03-14 14:34:32.360 6545-6545/com.couragedigital.petapp E/RecyclerView﹕ No adapter attached; skipping layout
03-14 14:34:32.391 6545-6545/com.couragedigital.petapp E/RecyclerView﹕ No adapter attached; skipping layout
03-14 14:34:32.425 6545-6545/com.couragedigital.petapp E/RecyclerView﹕ No adapter attached; skipping layout
03-14 14:34:32.433 6545-6545/com.couragedigital.petapp E/RecyclerView﹕ No adapter attached; skipping layout
03-14 14:35:00.626 6545-6545/com.couragedigital.petapp E/RecyclerView﹕ No adapter attached; skipping layout
我的Recyclerview
recyclerView = (RecyclerView) findViewById(R.id.clinicRateNReview);
recyclerView.setHasFixedSize(true);
//recyclerView.setNestedScrollingEnabled(false);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
reviewAdapter = new ClinicReviewsListAdapter(clinicReviewsListItemsArrayList);
recyclerView.setAdapter(reviewAdapter);
之后我从服务器获取列表并将其添加到列表并使用reviewAdapter.notifyDataSetChanged()
通知适配器。
以下是我的Clinic Review适配器代码
public class ClinicReviewsListAdapter extends RecyclerView.Adapter<ClinicReviewsListAdapter.ViewHolder>{
List<ClinicReviewsListItems> clinicReviewsListsItem;
View v;
ViewHolder viewHolder;
public ClinicReviewsListAdapter(List<ClinicReviewsListItems> clinicReviewsListArrayList) {
this.clinicReviewsListsItem = clinicReviewsListArrayList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.clinic_review_list_items, viewGroup, false);
viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
ClinicReviewsListItems clinicReviewsListItems = clinicReviewsListsItem.get(i);
viewHolder.bindClinicReviewsList(clinicReviewsListItems);
}
@Override
public int getItemCount() {
return clinicReviewsListsItem.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView clinicReviews;
public TextView email;
public View clinicListDividerLine;
public int userRatings;
LinearLayout layout;
public ViewHolder(View itemView) {
super(itemView);
layout = (LinearLayout) itemView.findViewById(R.id.ratingNos);
clinicReviews = (TextView) itemView.findViewById(R.id.reviews);
email = (TextView) itemView.findViewById(R.id.usersName);
clinicListDividerLine = itemView.findViewById(R.id.viewdividerline);
}
public void bindClinicReviewsList(ClinicReviewsListItems clinicReviewsList) {
userRatings=Integer.parseInt(clinicReviewsList.getClinicRatings());
for(int i=0;i<userRatings;i++)
{
ImageView image = new ImageView(v.getContext());
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
image.setId(i);
image.setImageResource(R.drawable.ratingstar_yellow);
// Adds the view to the layout
image.setPadding(3, 3, 3, 3);
layout.addView(image);
}
clinicReviews.setText(clinicReviewsList.getClinicReviews());
email.setText(clinicReviewsList.getEmail());
clinicListDividerLine.setBackgroundResource(R.color.list_internal_divider);
}
}
}
诊所recyclerview xml
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/clinicRateNReview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:clipToPadding="false"/>
recyclerview list item xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reviewLayout"
android:background="#FFFFFF">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reviews"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/reviews"
android:layout_marginTop="16dp"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rating:"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/ratingLabel"
android:layout_marginTop="10dp"
android:layout_below="@+id/reviews"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="112dp"
android:layout_alignTop="@+id/ratingLabel"
android:id="@+id/ratingNos"
android:orientation="horizontal">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="By:"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/userNameLabel"
android:layout_below="@+id/ratingNos"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="@+id/usersName"
android:layout_alignTop="@+id/userNameLabel"
android:layout_alignStart="@+id/ratingNos"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
<View
android:id="@+id/viewdividerline"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:background="@color/drawer_header_text"
android:layout_below="@id/reviewLayout"
android:layout_centerHorizontal="true"/>
</LinearLayout>
答案 0 :(得分:0)
添加:
recyclerView.setAdapter(reviewAdapter);
在notifydatasetchanged之前或在适配器创建之后。