I am taking data from "https://jsonplaceholder.typicode.com/todos" with using Retrofit, and I want to create RecyclerView with these data but my app is crashing.
What is the problem exactly? I also debuged app, I am taking all data from web but cannot put them into the recyclerview..
This is my adapter class
public class RecylerViewAdapter extends RecyclerView.Adapter<RecylerViewAdapter.MyViewHolder> {
List<Bilgiler> bilgilerList;
public RecylerViewAdapter(List<Bilgiler> bilgilerList) {
this.bilgilerList = bilgilerList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.userid.setText(bilgilerList.get(position).getUserId());
holder.id.setText(bilgilerList.get(position).getId());
holder.title.setText(bilgilerList.get(position).getTitle());
holder.checkBox.setChecked(bilgilerList.get(position).getCompleted());
}
@Override
public int getItemCount() {
return bilgilerList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView userid,id,title;
CheckBox checkBox;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
userid = itemView.findViewById(R.id.userid);
id = itemView.findViewById(R.id.id);
title = itemView.findViewById(R.id.titlee);
checkBox= itemView.findViewById(R.id.checkBox);
}
}
}
Finally, this is my Main class
public class MainActivity extends AppCompatActivity {
private List<Bilgiler> bilgilerList;
private RecyclerView recyclerView;
private RecylerViewAdapter recylerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyler);
take();
}
public void take() {
Call<List<Bilgiler>> bilgiList = ManagerAll.getInstance().getirBilgileri();
bilgiList.enqueue(new Callback<List<Bilgiler>>() {
@Override
public void onResponse(Call<List<Bilgiler>> call, Response<List<Bilgiler>> response) {
if (response.isSuccessful()) {
bilgilerList=response.body();
recylerViewAdapter= new RecylerViewAdapter(bilgilerList);
recyclerView.setAdapter(recylerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
Log.i("xxx",response.body().toString());
}
}
@Override
public void onFailure(Call<List<Bilgiler>> call, Throwable t) {
}
});
}
}
This is XML of page.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="300dp"
android:background="@color/purple_200"
>
<TextView
android:id="@+id/userid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="177dp"
android:layout_marginTop="39dp"
android:layout_marginEnd="177dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="178dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="176dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/userid" />
<TextView
android:id="@+id/titlee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="183dp"
android:layout_marginTop="31dp"
android:layout_marginEnd="170dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="155dp"
android:layout_marginTop="21dp"
android:layout_marginEnd="162dp"
android:text="CheckBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titlee" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try calling the notifyDataSetChanged
method on the adapter after getting the new data
bilgiList.enqueue(new Callback<List<Bilgiler>>() {
@Override
public void onResponse(Call<List<Bilgiler>> call, Response<List<Bilgiler>> response) {
if (response.isSuccessful()) {
bilgilerList=response.body();
recylerViewAdapter = new RecylerViewAdapter(bilgilerList);
recyclerView.setAdapter(recylerViewAdapter);
recylerViewAdapter.notifyDataSetChanged();
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
Log.i("xxx",response.body().toString());
}
}
@Override
public void onFailure(Call<List<Bilgiler>> call, Throwable t) {
}
});
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.