1

I am trying to create cv app in my RecyclerViewdadapter class I want to pass list and one from model class I already passed and second one for dummy data. I am following this stackoverflow link Two ArrayList one RecyclerView Adapter

I want to achieve that this ui

ui I want

second current screenshot

current UI

below MyAdapter class

public class EducationAdapter extends RecyclerView.Adapter<EducationAdapter.ViewHolder> {

    final int Internet_TYPE = 0;
    final int Dummy_TYPE = 1;
    public List<Education> educationList;
    public Context context;
    public List<FakeData> fakeData;
    public int[] subjectImage;
    public String[] subjectText;

    public EducationAdapter(List<Education> educationList, Context context, List<FakeData> fakeData, int[] subjectImage, String[] subjectText) {
        this.educationList = educationList;
        this.context = context;
        this.fakeData = fakeData;
        this.subjectImage = subjectImage;
        this.subjectText = subjectText;

    }

    @NonNull
    @Override
    public EducationAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == Internet_TYPE) {
            View itemView = LayoutInflater.from(context)
                    .inflate(R.layout.education_item, parent, false);  // change


            return new EducationAdapter.ViewHolder(itemView);
        }
        if (viewType == Dummy_TYPE) {
            return new ViewHolder.FakeViewHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull EducationAdapter.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            Education education = educationList.get(position);
            holder.duration.setText(education.getDuration());
            holder.degree.setText(education.getDegree());
            holder.institution.setText(education.getInstitution());

        }
        if (holder instanceof ViewHolder.FakeViewHolder) {

        }

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView duration, institution, degree, educationInfo, subjects, computers_science;
        ;
        private ImageView educationImage, subjectImage, computerScience;

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

            duration = (TextView) view.findViewById(R.id.duration);
            institution = (TextView) view.findViewById(R.id.institution);
            degree = (TextView) view.findViewById(R.id.degree);
            educationImage = (ImageView) view.findViewById(R.id.educationImage);
            educationInfo = (TextView) view.findViewById(R.id.education_info);
            subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
            subjects = (TextView) view.findViewById(R.id.subjects);


        }

        public class FakeViewHolder extends RecyclerView.ViewHolder {

            public TextView item;
            public ImageView icon;

            public FakeViewHolder(View itemView) {
                super(itemView);

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

            public void populate(FakeData fakeDatas) {
                item.setText(fakeDatas.getImage());
                icon.set
                dataSnippet.setText(imageDataWrapper.getPage_Desc());
                Picasso.with(context).load(imageDataWrapper.getPage_ImageThumb()).into(image);
            }

        }
    }
}

below my EducationItem where I have implemented network call and dummy data

public class EducationItem extends AppCompatActivity {

    private EducationAdapter educationAdapter;
    public List<Education> educationList;
    public List<FakeData> fakeData;
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    setContentView(R.layout.education);


       final  int [] subjectImage = {R.drawable.computer_science,
                R.drawable.data_structure,

        };




        final String[] subjectText = {
                "Computer Science",
                "Data Structure",

        };

    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();
            RecyclerView recyclerView  =  findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            educationAdapter = new EducationAdapter(educationList, EducationItem.this, fakeData, subjectImage, subjectText); // changes
            recyclerView.setAdapter(educationAdapter);
        }

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

        }
    });
}
}

below educution_item.

<?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="wrap_content"
    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" />


    <Space
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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_subjects"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <include
                layout="@layout/subject_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/subjects"
                android:layout_marginTop="60dp" />
        </RelativeLayout>


    </LinearLayout>

</LinearLayout>

below subjectList

<?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: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> 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

You should join two list in one list containing all items. Handling two different list in one adapter could make many different problems.

private void setAdapter() {
    List<Item> items = new ArrayList<>();
    items.add(new EducationItem());
    items.add(new FakeItem());
    recycler.setAdapter(newAdapter(items));
}

public interface Item {}

class EducationItem implements Item {
    //...fields
}

class FakeItem implements Item {
    //...fields
}
Marek Kondracki
  • 1,372
  • 2
  • 8
  • 13
0

As Marek said, you should join two list into one. Treat Education and FakeData as the same data type.

Using this approach, you should also override the getViewType method to indicate the ViewType of the item, so that the onCreateViewHolder can receive the ViewType you defined.