recyclerview无法投射UniversityViewHolder'?

时间:2018-10-31 15:44:30

标签: android types android-recyclerview multiple-views

亲爱的Android开发人员,我正在开发简历,我想在下面实现ui ui want 但是当前的UI看起来像这样,我在tutorial之后在RecyclerView中实现了多种视图类型 但是我不知道为什么UI在下面显示

current ui

要点https://gist.github.com/kyodgorbek/3bcd877832f127fbf8b750b49c2b2a47下方

在我实现了多个ViewType的EducationAdapter下方 公共类EducationAdapter扩展了RecyclerView.Adapter {

public Context context;
public List<Education> educationList;


private EducationItem educationItem;

public static class EducationViewHolder extends RecyclerView.ViewHolder {
    public TextView duration, institution, degree;


    public EducationViewHolder(View view) {
        super(view);

        duration = (TextView) itemView.findViewById(R.id.duration);
        institution = (TextView) itemView.findViewById(R.id.institution);
        degree = (TextView) itemView.findViewById(R.id.degree);


    }
}

public static class SubjectViewHolder extends RecyclerView.ViewHolder {
    public ImageView subjectImage;
    public TextView subjects;

    public SubjectViewHolder(@NonNull View itemView) {
        super(itemView);


        subjectImage = (ImageView) itemView.findViewById(R.id.subjectImage);
        subjects = (TextView) itemView.findViewById(R.id.subjects);


    }
}

public static class UniversityViewHolder extends RecyclerView.ViewHolder {
    public ImageView icon;
    public TextView item;

    public UniversityViewHolder(@NonNull View itemView) {
        super(itemView);


        icon = (ImageView) itemView.findViewById(R.id.icon);
        item = (TextView) itemView.findViewById(R.id.item);


    }
}


public EducationAdapter(List<Education> educationList, EducationItem educationItem, Context context) {

    this.educationList = educationList;
    this.context = context;

    this.educationItem = educationItem;

}


@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView;
    switch (viewType) {

        case INTERNET_TYPE:
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.education_item, parent, false);


            return new EducationViewHolder(itemView);

        case SUBJECT_TYPE:
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.subject, parent, false);


            return new SubjectViewHolder(itemView);
        case UNIVERSITY_TYPE:
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.subject_list, parent, false);
            return new UniversityViewHolder(itemView);


    }
    return null;
}

@Override
public int getItemViewType(int position) {


            return educationList.get(position).type;
    }


@Override
public int getItemCount() {
    return educationList.size();
}


@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

    Education education = educationList.get(position);
    if (education != null) {
        switch (education.type()) {
            case Education.INTERNET_TYPE:
                ((EducationViewHolder) holder).duration.setText(education.getInstitution());
                ((EducationViewHolder) holder).degree.setText(education.getDegree());
                ((EducationViewHolder) holder).institution.setText(education.getInstitution());
                break;


            case Education.SUBJECT_TYPE:
                ((SubjectViewHolder) holder).subjects.getContext().getString(R.string.university_subject);
                ((SubjectViewHolder) holder).subjectImage.getContext().getDrawable(R.drawable.university_subjects);
                break;


            case Education.UNIVERSITY_TYPE:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    (( UniversityViewHolder) holder).icon.setImageResource(education.image);
                }
                ((UniversityViewHolder) holder).item.setText(education.words);


                break;

        }
    }


}

}

在我进行网络通话的EducationItem下方

public class EducationItem extends AppCompatActivity {

    private EducationAdapter educationAdapter;
    public List<Education> educationList = new ArrayList<>();
    Context context;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.education);


        KitabInterface kitabInterface = ApiClient.getApiService();
        Call<KitabSawti> call = kitabInterface.getEducation();

        call.enqueue(new Callback<KitabSawti>() {
            @Override
            public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {

                educationList = response.body().getEducation();
                educationList.add(new Education("Computer Science", R.drawable.computer_science, Education.UNIVERSITY_TYPE));
                educationList.add(new Education("Data Structure", R.drawable.data_structure, Education.UNIVERSITY_TYPE));


                RecyclerView recyclerView = findViewById(R.id.recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                educationAdapter = new EducationAdapter(educationList, EducationItem.this, context); // changes
                recyclerView.setAdapter(educationAdapter);
            }

            @Override
            public void onFailure(Call<KitabSawti> call, Throwable t) {

            }
        });
    }
}

在我实现了recyclerview的education.xml下面

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">




    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:scrollbars="horizontal"
        android:id="@+id/recyclerView"
        android:layout_height="match_parent"/>



</RelativeLayout>

education_list.xml以下

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorBlust"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/educationImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="5dp"
                android:layout_marginLeft="5dp"
                android:src="@drawable/education_information"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/education_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:text="@string/education_information"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

        </LinearLayout>

        <TextView
            android:id="@+id/duration"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="12dp"
            android:layout_marginLeft="12dp"
            android:text="@string/text_duration"
            android:textColor="@color/colorWhite"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/institution"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="12dp"
            android:layout_marginLeft="12dp"
            android:text="@string/text_institution"
            android:textColor="@color/colorWhite"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/degree"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="12dp"
            android:layout_marginLeft="12dp"
            android:text="@string/text_degree"
            android:textColor="@color/colorWhite"
            android:textSize="16sp" />






    </LinearLayout>

subject.xml下面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/colorBlust"
    android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/subjectImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:src="@drawable/university_subjects"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/subjects"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="100dp"
                android:layout_marginLeft="100dp"
                android:text="@string/university_subject"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />
        </RelativeLayout>
</LinearLayout>

subject_list.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="match_parent"
    android:background="@color/colorBlust"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_marginLeft="10dp"
        android:layout_height="60dp"
        android:src="@drawable/computer_science"
        android:padding="5dp"
        android:layout_marginStart="10dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/computers_science"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="@color/colorWhite" />

    </LinearLayout>



</LinearLayout>

below Education.java model class
public class Education {

    public static final int UNIVERSITY_TYPE=0;
    public static final int INTERNET_TYPE = 1;
    public static final int SUBJECT_TYPE=2;


    public int type;
    public int image;
    public String words;

    public Education(String words, int image, int type) {
       this.words = words;
        this.image = image;
        this.type = type;

    }
    @SerializedName("duration")
    @Expose
    private String duration;
    @SerializedName("institution")
    @Expose
    private String institution;
    @SerializedName("degree")
    @Expose
    private String degree;



    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getInstitution() {
        return institution;
    }

    public void setInstitution(String institution) {
        this.institution = institution;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public int type() {
     return type;
     }
} 

在我的Education.java类下面

public class Education {

    public static final int UNIVERSITY_TYPE=0;
    public static final int INTERNET_TYPE = 1;
    public static final int SUBJECT_TYPE=2;


    public int type;
    public int image;
    public String words;

    public Education(String words, int image, int type) {
       this.words = words;
        this.image = image;
        this.type = type;

    }
    @SerializedName("duration")
    @Expose
    private String duration;
    @SerializedName("institution")
    @Expose
    private String institution;
    @SerializedName("degree")
    @Expose
    private String degree;



    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getInstitution() {
        return institution;
    }

    public void setInstitution(String institution) {
        this.institution = institution;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }

    public int type() {
     return type;
     }
}

我在调试模式下设置了断点并运行代码,但出现了异常

无法将'activity.drawer.navigation.com.kitabsawticlone.EducationAdapter $ UniversityViewHolder'强制转换为'activity.drawer.navigation.com.kitabsawticlone.EducationAdapter.EducationViewHolder'

1 个答案:

答案 0 :(得分:0)

首先,您不应在网络呼叫响应时设置您的回收站视图和适配器。您应该将所有这些设置为活动的一部分。

第二,您应该将模型添加/更新/删除到适配器中以作为网络的响应,然后调用notifydatasetchanged或其中的一些变体。

第三,这里有一些错误

case Education.SUBJECT_TYPE: ((SubjectViewHolder)holder).subjects.getContext().getString(R.string.university_subject);
((SubjectViewHolder)holder).subjectImage.getContext().getDrawable(R.drawable.university_subjects); break;