当前位置: 代码迷 >> java >> E / RecyclerView:未连接适配器; 跳过布局4
  详细解决方案

E / RecyclerView:未连接适配器; 跳过布局4

热度:64   发布时间:2023-07-16 17:32:35.0

RecyclerView上未显示数据。

我已经尽力了,但是即使我在StackoverFlow和其他平台上也搜索了很多问题,也无法获得解决方案。 我想像下面的图片一样做:

这是代码:

activity_data_sekretaris.xml

    <?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=".Sekretaris.DataSekretarisActivity"
    android:background="@color/colorPrimary">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rc_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

SekretarisAdapter.java

package tgs.app.absensi;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import tgs.app.absensi.model.Sekretaris;

public class SekretarisAdapter extends RecyclerView.Adapter<SekretarisAdapter.ViewHolder> {

private List<Sekretaris.Siswa> dataSiswa;
private List<Sekretaris.Kelas> dataKelas;

public SekretarisAdapter(List<Sekretaris.Siswa> dataSiswa, List<Sekretaris.Kelas> dataKelas) {
    this.dataSiswa = dataSiswa;
    this.dataKelas = dataKelas;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_sekretaris, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    viewHolder.text_no.setText(dataSiswa.get(i).getId_siswa());
    viewHolder.text_nis.setText(dataSiswa.get(i).getNIS());
    viewHolder.text_nama.setText(dataSiswa.get(i).getNama_lengkap());
    viewHolder.text_jenisk.setText(dataSiswa.get(i).getJenis_kelamin());
    viewHolder.text_kelas.setText(dataKelas.get(i).getNama_kelas());
    viewHolder.text_jurusan.setText(null);
    viewHolder.text_status.setText(dataSiswa.get(i).getStatus());
}

@Override
public int getItemCount() {
    return dataSiswa.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView text_no, text_nis, text_nama, text_jenisk, text_kelas, text_jurusan, text_status;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        text_no = itemView.findViewById(R.id.text_no);
        text_nis = itemView.findViewById(R.id.text_nis);
        text_nama = itemView.findViewById(R.id.text_nama);
        text_jenisk = itemView.findViewById(R.id.text_jenisKelamin);
        text_kelas = itemView.findViewById(R.id.text_kelas);
        text_jurusan = itemView.findViewById(R.id.text_jurusan);
        text_status = itemView.findViewById(R.id.text_status);
    }
}

}

DataSekretarisActivity.java

package tgs.app.absensi.Sekretaris;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import tgs.app.absensi.R;
import tgs.app.absensi.SekretarisAdapter;
import tgs.app.absensi.model.Sekretaris;
import tgs.app.absensi.retrofit.Api;
import tgs.app.absensi.retrofit.ApiInterface;

public class DataSekretarisActivity extends AppCompatActivity {

SwipeRefreshLayout swipe_refresh;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_sekretaris);

    swipe_refresh = findViewById(R.id.swipe_refresh);
    recyclerView = findViewById(R.id.rc_data);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);

    DataSekretaris();
    swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipe_refresh.setRefreshing(true);
            DataSekretaris();
        }
    });


    getSupportActionBar().setTitle("Data Absen Siswa");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

private void DataSekretaris(){
    ApiInterface apiInterface = Api.getUrl().create(ApiInterface.class);
    Call<Sekretaris> call = apiInterface.getSekretarisCall();
    call.enqueue(new Callback<Sekretaris>() {
        @Override
        public void onResponse(Call<Sekretaris> call, Response<Sekretaris> response) {
            swipe_refresh.setRefreshing(false);
            List<Sekretaris.Siswa> detailSiswa = response.body().getReadSiswa();
            List<Sekretaris.Kelas> detailKelas = response.body().getReadKelas();
            recyclerView.setAdapter(new SekretarisAdapter(detailSiswa, detailKelas));
        }

        @Override
        public void onFailure(Call<Sekretaris> call, Throwable t) {

        }
    });
}

@Override
public boolean onSupportNavigateUp() {
    finish();
    return super.onSupportNavigateUp();
}

}

谁能解决我的错误? 提前致谢。

您应该首先正确设置RecyclerView,例如@AndrésR.C。 说,通过使用默认适配器。

因此,请使用以下内容初始化RecyclerView:

private RecyclerView mRvSekretaris;
private SekretarisAdapter mSkretarisAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    mRvSekretaris = findViewById(R.id.rc_data);
    mRvSekretaris.setLayoutManager(new LinearLayoutManager(this));

    // initialize list with empty item
    List<Sekretaris.Siswa> detailSiswa = new ArrayList<>();
    List<Sekretaris.Kelas> detailKelas = new ArrayList<>();
    mSkretarisAdapter = new SekretarisAdapter(detailSiswa, detailKelas);

    mRvSekretaris.setAdapter(mSkretarisAdapter);

然后,当您要刷新列表时,只需在适配器中将项目适配器与swap方法交换:

mSkretarisAdapter.swap(detailSiswa, detailKelas);

使用以下swap方法:

public class SekretarisAdapter extends RecyclerView.Adapter<SekretarisAdapter.ViewHolder> {

    private List<Sekretaris.Siswa> dataSiswa;
    private List<Sekretaris.Kelas> dataKelas;

    ...

    public swap(List<Sekretaris.Siswa> dataSiswa, List<Sekretaris.Kelas> dataKelas) {

      this.dataSiswa = dataSiswa;
      this.dataKelas = dataKelas;
      notifyDataSetChanged();
    }
}

请注意 ,您不应该交换所有项目,而需要使用类的

您可以在进行网络调用和传递空列表以及将两个列表对象都设置为全局之前设置适配器

// initialize list with empty item
 detailSiswa = new ArrayList<>();
 detailKelas = new ArrayList<>()
mRvSekretaris.setLayoutManager(new LinearLayoutManager(this));
mSkretarisAdapter = new SekretarisAdapter(detailSiswa, detailKelas);
mRvSekretaris.setAdapter(mSkretarisAdapter);

当您要刷新列表时,只需调用refreshLists(dataSiswa,dataKelas);即可。

private void refreshLists(ArrayListList<Sekretaris.Siswa> dataSiswa, ArrayListList<Sekretaris.Kelas> dataKelas){
    detailSiswa.clear();
    detailKelas.clear();
    if(dataSiswa.size>0 && dataKelas.size>0){
       detailSiswa.add(dataSiswa);
       detailKelas.add(dataKelas);
    }
    mSkretarisAdapter.notifyDataSetChanged();
}

此外,在BindViewHolder中的列表中设置数据时,请确保dataKelas的大小等于或大于dataSiswa,因为BindViewHolder的调用次数与dataSiswa的大小相同,因为

public int getItemCount() {
return dataSiswa.size();

}

因此,当您从dataKelas访问任何字段时,如果dataKelas的大小小于dataSiswa的大小,则可能引发null指针异常。

  相关解决方案