繁体   English   中英

即使我初始化了适配器,也没有附加适配器跳过布局

[英]No adapter attached skipping layout even though I initialized the adapter

我在聊天片段中遇到了 RecyclerView 的问题。 我正在尝试在片段上显示最近的聊天。 聊天列表在 firebase 中创建:

Firebase 聊天列表表

我在我的 logcat 中获得了药房的名称。

但是,我仍然无法在我的回收站视图中看到药房列表。

这是我的代码:

聊天片段

public class chatsFragment extends Fragment {
private PharmacieAdapter pharmacieAdapter;
private List<Pharmacie> mPh;
private RecyclerView chatrv;
FirebaseDatabase firebaseDatabase;
DatabaseReference reference;
DatabaseReference refPh;
private List<ChatList> phList;


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
   View v = inflater.inflate(R.layout.fragment_chats, container, false);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new
                StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
  chatrv = (RecyclerView)v.findViewById(R.id.msg_rv);
    Log.i("inside", "rv initiated" );
  // chatrv = v.findViewById(R.id.msg_rv);
   chatrv.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
   chatrv.setLayoutManager(linearLayoutManager);



   firebaseDatabase = FirebaseDatabase.getInstance();
   reference = firebaseDatabase.getReference().child(UserPhoneKey);
    Log.i("inside", reference.toString());

   phList = new ArrayList<>();
   refPh = firebaseDatabase.getReference("ChatList").child(UserPhoneKey);
   refPh.addValueEventListener(new ValueEventListener() {
       @Override
       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
           phList.clear();
           for(DataSnapshot snapshot:dataSnapshot.getChildren()){
               ChatList chatList = snapshot.getValue(ChatList.class);
               System.out.println(chatList);
               phList.add(chatList);
           }

          ChatsList();

       }

       @Override
       public void onCancelled(@NonNull DatabaseError databaseError) {

       }
   });

    if (phList.isEmpty())
        chatrv.setVisibility(View.VISIBLE);
    else
        chatrv.setVisibility(View.GONE);

    return v;

}

private void ChatsList() {
    //get all recent chats

    mPh = new ArrayList<>();
    refPh = FirebaseDatabase.getInstance().getReference("Pharmacie");
    refPh.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                phList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Pharmacie ph = snapshot.getValue(Pharmacie.class);
                    for (ChatList chatList : phList) {
                        if (ph.getPhone().equals(chatList.getId())) {
                            System.out.println(ph);
                            mPh.add(ph);
                        }
                    }

                }

                pharmacieAdapter = new PharmacieAdapter(getContext(), mPh);
                chatrv.setAdapter(pharmacieAdapter);
                pharmacieAdapter.notifyDataSetChanged();
            } else {
                Toast.makeText(getActivity(), "NO RESULT!", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            System.out.println("The read failed"+ databaseError.getCode() );
        }
    });
}}

药剂适配器 class

public class PharmacieAdapter extends RecyclerView.Adapter<PharmacieAdapter.ViewHolder> {
private Context context;
private List <Pharmacie> phUser;

public PharmacieAdapter(Context Context, List<Pharmacie> mPharmacie) {
    context = Context;
    phUser = mPharmacie;
}

public PharmacieAdapter() {

}



@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.ph_user,parent,false);
    return new PharmacieAdapter.ViewHolder(view);
}

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


    Pharmacie pharmacies = phUser.get(position);
    holder.pharmacieName.setText(pharmacies.getNom());
    holder.adressePharmacie.setText(pharmacies.getAdresse());


    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PharPhoneKey = String.valueOf(pharmacies.getPhone());
            Intent i = new Intent(context, MessageActivity.class);
            i.putExtra("Nom",pharmacies.getNom());
            context.startActivity(i);
        }
    });
}

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


public  class ViewHolder extends RecyclerView.ViewHolder{
    public TextView pharmacieName,adressePharmacie;



    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        pharmacieName = itemView.findViewById(R.id.PhName);
        adressePharmacie= itemView.findViewById(R.id.PhAdresse);

    }




}}

聊天片段

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".chatApp.Fragment.chatsFragment">
   <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/msg_rv"/>
  </FrameLayout>

聊天列表.class

public class ChatList {
private String id;

public ChatList() {
}

public ChatList(String id) {
    this.id = id;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}}
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:id="@+id/msg_rv"/>
 **app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"**

在 xml 中尝试这条线这对我有用

暂无
暂无

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

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