我对聊天片段中的recyclerview有问题。我想展示一下最近关于一个片段的聊天。聊天列表是在firebase中创建的:
我把药店的名字记在日志里了。
但是,我仍然无法在我的回收者视图中看到药店列表。
这是我的密码:
聊天片段
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() );
}
});
}}
PharmaceAdapter类
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>
chatlist.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;
}}
1条答案
按热度按时间uubf1zoe1#
试试xml中的这一行这对我有用