我正在尝试实现要在虚拟/ POC RecyclerView应用程序上使用的自定义RecyclerView.Adaper。我正在使用this教程。我的模型称为Movie
,其中有两个字段:
@NonNull
@PrimaryKey
private String name;
@NonNull
private Integer releaseYear;
所以我有这堂课
MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder>
覆盖onCreateViewHolder
,onBindViewHolder
和getItemCount
并定义MyViewHolder extends RecyclerView.ViewHolder
。
当调试行位于onBindViewHolder的最后一行时:
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(@NonNull MyViewHolder viewHolder, int position) {
// Get the data model based on position
Movie movie = movies.get(position);
// Set item views based on your views and data model
TextView tvMovieName = viewHolder.tvMovieName;
tvMovieName.setText(movie.getName());
TextView tvMovieReleaseYear = viewHolder.tvMovieReleaseYear;
tvMovieReleaseYear.setText(movie.getReleaseYear());
}
我知道
Process: br.com.basicfeatures, PID: 2555
android.content.res.Resources$NotFoundException: String resource ID #0x7cf
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.TextView.setText(TextView.java:5831)
at br.com.basicfeatures.presentation.adapter.MovieAdapter.onBindViewHolder(MovieAdapter.java:45)
at br.com.basicfeatures.presentation.adapter.MovieAdapter.onBindViewHolder(MovieAdapter.java:15)
这是MyViewHolder:
protected class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvMovieName;
private TextView tvMovieReleaseYear;
private MyViewHolder(@NonNull View itemView) {
super(itemView);
this.tvMovieName = itemView.findViewById(R.id.id_tv_movie_name);
this.tvMovieReleaseYear = itemView.findViewById(R.id.id_tv_movie_release_year);
}
}
答案 0 :(得分:1)
这是因为movie.getReleaseYear()
返回一个整数,所以tvMovieReleaseYear.setText
试图查找不存在的相应资源文件。因此,您需要将movie.getReleaseYear()
转换为字符串
tvMovieReleaseYear.setText(movie.getReleaseYear() + "");
答案 1 :(得分:1)
将movie.getReleaseYear()
的值转换为String
。
tvMovieReleaseYear.setText(String.valueOf(movie.getReleaseYear()));
为什么需要?
TextView
中只有2个方法在setText()
中采用整数,并且都期望StringRes
。因此,当您尝试设置整数时,它假定您正在尝试从String资源中设置一个字符串。
因此,要使用setText()
设置整数,您需要以任何方式将其转换为字符串。
下面是我上面提到的两种方法
/**
5821 * Sets the text to be displayed using a string resource identifier.
5822 *
5823 * @param resid the resource identifier of the string resource to be displayed
5824 *
5825 * @see #setText(CharSequence)
5826 *
5827 * @attr ref android.R.styleable#TextView_text
5828 */
5829 @android.view.RemotableViewMethod
5830 public final void setText(@StringRes int resid) {
5831 setText(getContext().getResources().getText(resid));
5832 mTextSetFromXmlOrResourceId = true;
5833 mTextId = resid;
5834 }
5835
5836 /**
5837 * Sets the text to be displayed using a string resource identifier and the
5838 * {@link android.widget.TextView.BufferType}.
5839 * <p/>
5840 * When required, TextView will use {@link android.text.Spannable.Factory} to create final or
5841 * intermediate {@link Spannable Spannables}. Likewise it will use
5842 * {@link android.text.Editable.Factory} to create final or intermediate
5843 * {@link Editable Editables}.
5844 *
5845 * @param resid the resource identifier of the string resource to be displayed
5846 * @param type a {@link android.widget.TextView.BufferType} which defines whether the text is
5847 * stored as a static text, styleable/spannable text, or editable text
5848 *
5849 * @see #setText(int)
5850 * @see #setText(CharSequence)
5851 * @see android.widget.TextView.BufferType
5852 * @see #setSpannableFactory(Spannable.Factory)
5853 * @see #setEditableFactory(Editable.Factory)
5854 *
5855 * @attr ref android.R.styleable#TextView_text
5856 * @attr ref android.R.styleable#TextView_bufferType
5857 */
5858 public final void setText(@StringRes int resid, BufferType type) {
5859 setText(getContext().getResources().getText(resid), type);
5860 mTextSetFromXmlOrResourceId = true;
5861 mTextId = resid;
5862 }