我有一个带有viewholder的recyclerView,我试图将TextViews绑定到holder。我已经将我的ImageViews绑定得很好,但我的TextViews只显示错误。
@Override
public void onBindViewHolder(BreadSliceViewHolder holder, int position) {
/****************
* User Icon *
****************/
int userNameId = 0;
userNameId = ViewHolderUtilities
.getUserIcon(userNameId);
holder.iconView.setImageResource(userNameId);
/****************
* User Name*
****************/
int userName = 1;
userName = ViewHolderUtilities
.getUserName(userName);
holder.userName.setText(userName);
/****************
* User Crumbs*
****************/
int userCrumb = 2;
userCrumb = ViewHolderUtilities
.getUserCrumb(userCrumb);
holder.numberOfCrumbs.setText(userCrumb);
/****************
* Total Distance*
****************/
int totalDistance = 3;
totalDistance = ViewHolderUtilities
.getTotalDistance(totalDistance);
holder.totalDistance.setText(totalDistance);
}
这是Utilities类,我返回ID:
public class ViewHolderUtilities {
public static int getUserIcon(int userId) {
return R.drawable.man_stock;
}
public static int getUserCrumb() {
return R.id.number_of_crumbs;
}
public static int getTotalDistance() {
return R.id.total_distance;
}
public static int getUserName() {
return R.id.user_name;
}
}
这是TextViews的XML代码:
<ImageView
android:id="@+id/user_icon"
android:layout_width="@dimen/list_icon"
android:layout_height="@dimen/list_icon"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline"
tools:src="@drawable/man_stock" />
<ImageView
android:id="@+id/crumb_imageView"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:cropToPadding="false"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toRightOf="@+id/number_of_crumbs"
app:srcCompat="@drawable/breadcrumbs_crumb" />
<TextView
android:id="@+id/number_of_crumbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/list_item_date_left_margin"
android:layout_marginStart="@dimen/list_item_date_start_margin"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toRightOf="@+id/user_icon"
tools:text="@string/number_of_crumbs" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintLeft_toLeftOf="@+id/number_of_crumbs"
app:layout_constraintTop_toTopOf="@+id/guideline"
tools:text="@string/user_name" />
<TextView
android:id="@+id/total_distance"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textSize="10sp"
app:layout_constraintLeft_toRightOf="@+id/crumb_imageView"
app:layout_constraintTop_toTopOf="@+id/number_of_crumbs"
tools:text="@string/distance_traveled" />
这是设置视图/图像的代码部分
class BreadSliceViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
final ImageView iconView;
final ImageView crumbImageView;
final TextView userName;
final TextView numberOfCrumbs;
final TextView totalDistance;
BreadSliceViewHolder(View view) {
super(view);
iconView = (ImageView) view.findViewById(R.id.user_icon);
crumbImageView = (ImageView) view.findViewById(R.id.crumb_imageView);
numberOfCrumbs = (TextView) view.findViewById(number_of_crumbs);
userName = (TextView) view.findViewById(R.id.user_name);
totalDistance = (TextView) view.findViewById(R.id.total_distance);
感谢您一直看着这段时间。我很欣赏任何见解。