I have been trying to use recycler view to show some data in a list of textviews. But my code doesn't work. There are no error messages in the logcat. I had used recycler view in past but I don't know what the problem is.
package com.example.diffutil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Adaptershow extends RecyclerView.Adapter<Adaptershow.MyViewHolder> {
ArrayList<Student> mStudents;
public Adaptershow(ArrayList<Student> mStudents) {
this.mStudents = mStudents;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.show.setText(mStudents.get(position).getName());
}
@Override
public int getItemCount() {
return mStudents.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView show;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
show = itemView.findViewById(R.id.show);
}
}
}
diffutil is the name of the project.
MainActivity code below:-
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Student> students;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv);
students = new ArrayList<>();
students.add(new Student(1,"a","q"));
students.add(new Student(1,"b","w"));
students.add(new Student(1,"c","e"));
students.add(new Student(1,"d","r"));
students.add(new Student(1,"e","t"));
Adaptershow adapter = new Adaptershow(students);
recyclerView.setAdapter(adapter);
}
}
XML code below:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"/>
</RelativeLayout>
Item XML code:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="hello"/>
</RelativeLayout>
I don't see a LinearLayoutManager
for your recyclerview:
..........
..........
Adaptershow adapter = new Adaptershow(students);
//here
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.