如何在android中将web服务数据传递到recylerview?

vc9ivgsu  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(204)

“我正在从”https://jsonplaceholder.typicode.com/todos“使用改装,我想用这些数据创建recyclerview,但我的应用程序正在崩溃。
到底是什么问题?我还调试了应用程序,我从网上取得了所有的数据,但不能把他们放进recyclerview。。
这是我的适配器类

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);

        }
    }

}

最后,这是我的主要课程

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) {

            }
        });

    }

}

这是页面的xml。

<?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>
bvpmtnay

bvpmtnay1#

试着打电话给 notifyDataSetChanged 获取新数据后适配器上的方法

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) {

        }
    });

相关问题