0

I have a recyclerview in a fragment. And I am using butterknife library.

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/studentlist" />

then the adapter class will be

public class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.MyViewHolder> {
    private List<StudentData> dataList;
    private Context context;

    public PeopleAdapter(List<StudentData> data, Context context) {
        this.dataList = data;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.people_list_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        StudentData data=dataList.get(position);
        holder.email.setText(data.getEmail());
        holder.name.setText(data.getName());
        holder.phone.setText(data.getPhoneNo());
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.name)
        TextView name;
        @BindView(R.id.email)
        TextView email;
        @BindView(R.id.phone)
        TextView phone;
        @BindView(R.id.image)
        TextView image;
    public MyViewHolder(View view) {
        super(view);
        ButterKnife.bind(this,view);
    }
}

}

I Initialized recyclerview with

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(mLayoutManager);
        mRecyclerview.setAdapter(new PeopleAdapter(MyConfiguration.getTemData(),getActivity()));

the data generated method

public static List<StudentData> getTemData(){
        List<StudentData> dataList=new ArrayList<>();
        for (int i=0;i<10;i++){
            dataList.add(new StudentData("myemail"+i+"@gmail.com",i*45234523+"","myname"+i));
        }
        return dataList;
    }

list item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="50dp"
        app:srcCompat="@drawable/profilepic"
        android:id="@+id/image"
        android:layout_marginTop="5dp"
        android:layout_height="60dp" />

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/student_listcard_left_margin"
        android:layout_marginRight="@dimen/student_listcard_rightmargin"
        android:layout_marginTop="5dp">

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

            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:textSize="18sp"
                android:textColor="@android:color/black" />

            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/phone" />

            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_edit"
            android:id="@+id/imageView5"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

StudentData class

public class StudentData {
    private String name;
    private String email;

    public StudentData(String email, String phoneNo, String name) {
        this.email = email;
        this.phoneNo = phoneNo;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public StudentData() {

    }

    private String phoneNo;
    }

parent view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ex.ex.fragments.student.StudentInformation">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="false">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabSelectedTextColor="@android:color/white"
            app:tabMode="scrollable"/>


    </android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewpager" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout

So the problem is the recyclerview doesn't show it's items. Please Help me;

6
  • Also, share your Adapter people_list_item layout code Commented Jan 31, 2017 at 7:38
  • @mdg5435 ok..I have edited my question Commented Jan 31, 2017 at 7:39
  • & StudentData class too please. Commented Jan 31, 2017 at 7:41
  • @mdg5435 it's done.. Commented Jan 31, 2017 at 7:42
  • 1
    @CoDfather : I used your code and it's working fine for me Commented Jan 31, 2017 at 7:49

2 Answers 2

2

I don't have idea about ButterKnife but what I think is

following is imageview

<ImageView
    android:layout_width="50dp"
    app:srcCompat="@drawable/profilepic"
    android:id="@+id/image"
    android:layout_marginTop="5dp"
    android:layout_height="60dp" />

and you're binding it with textview

@BindView(R.id.image)
TextView image;

so replace it like

@BindView(R.id.image)
ImageView image;
2
  • this is a good point but I have 2 questions, wouldn't this throw a ClassCastException or something? and, why is the RecyclerView showing nothing, shouldn't we at least see the other fields?
    – lelloman
    Commented Jan 31, 2017 at 8:04
  • @CoDfather just checked and this error should have thrown a ClassCastException, so if you've not had any it means that no MyViewHolder are being created at all
    – lelloman
    Commented Jan 31, 2017 at 10:26
0

My mistake,which is ignored by me.

I have a viewpager in that I did all that stuff. but the viewpager enclosed by a nested scrollview which is sized to nothing..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ex.ex.fragments.student.StudentInformation">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="false">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabSelectedTextColor="@android:color/white"
            app:tabMode="scrollable"/>


    </android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewpager" />
</android.support.v4.widget.NestedScrollView>
</LinearLayout>

So I removed the parent of view pager and it became.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ex.ex.fragments.student.StudentInformation">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="false">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabSelectedTextColor="@android:color/white"
            app:tabMode="scrollable"/>


    </android.support.design.widget.AppBarLayout>


        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewpager" />
</LinearLayout>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.