gpt4 book ai didi

android - RecyclerView:没有附加适配器;跳过 fragment 中的布局

转载 作者:行者123 更新时间:2023-11-29 16:45:16 25 4
gpt4 key购买 nike

我一直在 StackOverflow 上阅读不同的答案并尝试实现他们的解决方案,但我仍然收到错误:RecyclerView:没有附加适配器;跳过布局,

所以我在我的 fragment 中的 onCreateView 中初始化我的回收器 View ,如下所示:

public class StatusFragment extends Fragment {

DatabaseReference databaseStatus;
ProgressDialog progressDialog;
List<ElectricityClass> list = new ArrayList<ElectricityClass>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;

public StatusFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_status, container, false);

recyclerView = rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);

progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading Data from Firebase Database");
progressDialog.show();

databaseStatus = FirebaseDatabase.getInstance().getReference().child("Electricity");
databaseStatus.addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot snapshot) {

for (DataSnapshot dataSnapshot : snapshot.getChildren()) {

ElectricityClass electricityClass = dataSnapshot.getValue(ElectricityClass.class);
list.add(electricityClass);
}
recyclerView.setLayoutManager(new LinearLayoutManager(getContext().getApplicationContext()));
adapter = new RecyclerViewAdapter(getContext().getApplicationContext(), list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}

@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});

return rootView;
}
}

我的 RecyclerViewAdapter 类:

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

Context context;
List<ElectricityClass> dataList;



public RecyclerViewAdapter(Context context, List<ElectricityClass> list) {

this.dataList = list;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

ElectricityClass studentDetails = dataList.get(position);
holder.StudentNameTextView.setText(studentDetails.getName());
holder.StudentNumberTextView.setText(studentDetails.getType());
}


@Override
public int getItemCount() {

return dataList.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

public TextView StudentNameTextView;
public TextView StudentNumberTextView;

public ViewHolder(View itemView) {

super(itemView);
StudentNameTextView = itemView.findViewById(R.id.ShowStudentNameTextView);
StudentNumberTextView = itemView.findViewById(R.id.ShowStudentNumberTextView);
}
}

我的列表电力类:

   class ElectricityClass {


private String id;
private String email;
private String name;
private String type;
private String detail;
private String location;
private String date;
private String imgurl;

public ElectricityClass() {

// Required empty public constructor
}


public ElectricityClass(String id, String currentUserString, String imageUrl, String nameString, String typeString, String detailString, String locationString, String dateString){

this.id = id;
this.email = currentUserString;
this.name =nameString;
this.type = typeString;
this.detail = detailString;
this.location = locationString;
this.date = dateString;
this.imgurl = imageUrl;
}

public String getImgurl() {
return imgurl;
}

public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

这是我在 fragment 中的布局:

 <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>

</LinearLayout>

以及项目的布局:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardElevation="5dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="5dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ECEFF1"
android:padding="10dp">

<TextView
android:id="@+id/StudentName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Student Name: "
android:textColor="#000"
android:textSize="20dp" />

<TextView
android:id="@+id/ShowStudentNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Show Student Name"
android:textColor="#000"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/StudentName"
android:layout_toEndOf="@+id/StudentName"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp" />

<TextView
android:id="@+id/StudentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Phone Number: "
android:textColor="#000"
android:textSize="20dp"
android:layout_below="@+id/StudentName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:id="@+id/ShowStudentNumberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Show Number"
android:textColor="#000"
android:textSize="20dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_below="@+id/ShowStudentNameTextView"
android:layout_toRightOf="@+id/StudentNumber"
android:layout_toEndOf="@+id/StudentNumber" />

</RelativeLayout>
</android.support.v7.widget.CardView>

现在显示,

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged()' on a null object reference

在这里我定义了我的 List2

  public class StatusFragment extends Fragment {

DatabaseReference databaseStatus;
ProgressDialog progressDialog;
List<ElectricityClass> list2 = new ArrayList<ElectricityClass>();
List<ElectricityClass> list = new ArrayList<ElectricityClass>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;

在 onDataChanged() 中,

   public void onDataChange(DataSnapshot snapshot) {

for (DataSnapshot dataSnapshot : snapshot.getChildren()) {

ElectricityClass electricityClass = dataSnapshot.getValue(ElectricityClass.class);
list2.add(electricityClass);
}
refreshRv((ArrayList<ElectricityClass>) list2);
adapter = new RecyclerViewAdapter(getContext().getApplicationContext(), list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}

private void refreshRv(ArrayList<ElectricityClass> list2){
list.clear();
list.addAll(list2);
}

最佳答案

您在 onDataChange 中错误地设置布局和适配器方法。而是创建一个私有(private)函数来实例化您的适配器,并将其设置为 RecyclerView并且也会设置布局管理器。 (你可能想要 define 来自适配器构造函数的列表也像这样 list = new ArrayList<>(); )

完成此函数后,在 onCreateView 中调用它 fragment 的方法,以及内部 onDataChange只需调用一个刷新函数(也是私有(private)的)来清除您的列表(或不清除取决于您的行为),添加所有新值并使用 adapter.notifyDataSetChanged() 通知您的适配器方法。

希望这对你有帮助:)

编辑:正如评论中提到的那样,您的错误只是告诉您适配器未设置为 RecyclerView ,可能是因为 onDataChanged()没有被调用。我上面的解释肯定能解决问题

编辑:定义另一个列表比方说 list2 , 替换 list.add();list2.add();在里面 for from onDataSetChanged();

然后for之后,调用这个函数

private void refreshRv(ArrayList<YourDataType> list2){
list.clear();
list.addAll(list2);
adapter.notifyDataSetChanged();
}

关于android - RecyclerView:没有附加适配器;跳过 fragment 中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48721470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com