infoOfPost.java
package com.sakib23.task1; public class infoOfPost { String UserName; String ProjectTitle, ProjectDescription, ProjectTime; boolean ProjectFlag; public infoOfPost(String userName, String projectTitle, String projectDescription, String projectTime, boolean projectFlag) { UserName = userName; ProjectTitle = projectTitle; ProjectDescription = projectDescription; ProjectTime = projectTime; ProjectFlag = projectFlag; } public String getUserName() { return UserName; } public void setUserName(String userName) { UserName = userName; } public String getProjectTitle() { return ProjectTitle; } public void setProjectTitle(String projectTitle) { ProjectTitle = projectTitle; } public String getProjectDescription() { return ProjectDescription; } public void setProjectDescription(String projectDescription) { ProjectDescription = projectDescription; } public String getProjectTime() { return ProjectTime; } public void setProjectTime(String projectTime) { ProjectTime = projectTime; } public boolean getProjectFlag() { return ProjectFlag; } public void setProjectFlag(boolean projectFlag) { ProjectFlag = projectFlag; } }
MainActivity.java
package com.sakib23.task1; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; ArrayList<infoOfPost> arrayList; myAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.RecyclerViewID); arrayList = new ArrayList<>(); arrayList.add(new infoOfPost("A B C", "B", "C", "01/02/12", true)); arrayList.add(new infoOfPost("B A A", "B", "C", "06/02/12", true)); arrayList.add(new infoOfPost("A B A", "B", "C", "01/03/12", false)); arrayList.add(new infoOfPost("C A A", "B", "C", "02/03/12", false)); arrayList.add(new infoOfPost("A A A", "B", "C", "01/09/12", true)); arrayList.add(new infoOfPost("A L A", "B", "C", "11/12/12", false)); adapter = new myAdapter(this, arrayList); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); } }
myAdapter.java
package com.sakib23.task1; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class myAdapter extends RecyclerView.Adapter { Context context; ArrayList arrayList; public myAdapter(Context context, ArrayList arrayList) { this.context = context; this.arrayList = arrayList; Log.d("TAGG", "Total Size: " + arrayList.size()); // Okay it is fine. } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.sample_post, viewGroup, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { Log.d("TAGG", "index " + i); // it is not incrementing myViewHolder.userName.setText(arrayList.get(i).getUserName()); myViewHolder.projectTitle.setText(arrayList.get(i).getProjectTitle()); myViewHolder.projectDescription.setText(arrayList.get(i).getProjectDescription()); myViewHolder.projectTime.setText(arrayList.get(i).getProjectTime()); if(arrayList.get(i).getProjectFlag() == false) myViewHolder.projectFlag.setText("Not Flagged Post"); else myViewHolder.projectFlag.setText("Flagged Post"); } @Override public int getItemCount() { Log.d("TAGG", " GETSIZE " + arrayList.size() ); // it is also fine.. return arrayList.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView userName, projectTitle, projectDescription, projectTime, projectFlag; public MyViewHolder(@NonNull View itemView) { super(itemView); userName = itemView.findViewById(R.id.usernameID); projectTitle = itemView.findViewById(R.id.projecttitleID); projectDescription = itemView.findViewById(R.id.projectdesriptionID); projectTime = itemView.findViewById(R.id.projecttimeID); projectFlag = itemView.findViewById(R.id.projectflagID); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerViewID"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
sample_post
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UserName"
android:id="@+id/usernameID"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProjectTitle"
android:id="@+id/projecttitleID"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProjectDescription"
android:id="@+id/projectdesriptionID"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProjectTime"
android:id="@+id/projecttimeID"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Flag"
android:id="@+id/projectflagID"
/>
</LinearLayout>
Here in the myAdapter.java file, onBindViewHolder index is not increasing. Please Help me.
recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
.. Then Check. – ADM