繁体   English   中英

如何将web服务数据传入Android中的RecylerView?

[英]How to pass web service data into the RecylerView in Android?

我正在使用 Retrofit 从“https://jsonplaceholder.typicode.com/todos”获取数据,我想用这些数据创建 RecyclerView,但我的应用程序崩溃了。

究竟是什么问题? 我还调试了应用程序,我正在从 web 中获取所有数据,但不能将它们放入 recyclerview 中。

这是我的适配器 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);

        }
    }


}

最后,这是我的主要 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) {

            }
        });

    }

}

这是页面的 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>

获取新数据后尝试在适配器上调用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) {

        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM