如何将 Web 服务数据传递到 Android 中的 RecyclerView?

Posted

技术标签:

【中文标题】如何将 Web 服务数据传递到 Android 中的 RecyclerView?【英文标题】:How to pass web service data into the RecylerView in Android? 【发布时间】:2021-04-01 13:31:36 【问题描述】:

我正在使用 Retrofit 从“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_
    android:layout_
    android:background="@color/purple_200"
    >

    <TextView
        android:id="@+id/userid"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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>

【问题讨论】:

【参考方案1】:

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

        
    );

【讨论】:

添加recylerViewAdapter.notifyDataSetChanged();后还是同样的问题 可能布局有问题,请将活动和适配器项的布局 XML 添加到问题中 我添加到布局 XML。

以上是关于如何将 Web 服务数据传递到 Android 中的 RecyclerView?的主要内容,如果未能解决你的问题,请参考以下文章

如何将来自 .NET Web 服务的 DataSet 绑定到 android GridView?

如何将多部分/表单数据从 android 发送到 Web 服务器?

在基于 Xamarin 表单的应用程序中将数据从 android 服务传递到 ContentPage

如何将数据从服务器传递到服务人员

如何将数据从服务器发送到Android?

android客户端如何提交表单数据给web服务器