Мое приложение содержит фрагмент MyPostFragment, в который вставляются все сообщения текущего пользователя.
Я хочу:
- показать запрос, отправленный другими пользователями наэтот пост при нажатии на родительский элемент RecyclerView и добавлении его в дочерний список / дочерний RecyclerView с помощью firebase firestore.
код
MyPostsFragment
public class MyPostsFragment extends Fragment {
private RecyclerView my_list;
private List<AllPosts> posts_list;
private List<String> accept_request;
private static final String TAG = "Log";
private FirebaseFirestore firebaseFirestore;
private String u_id, id, message, p_id, a, b;
private FirebaseAuth mAuth;
private UserRecyclerAdapter userRecyclerAdapter;
public MyPostsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
u_id = mAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_posts, container, false);
my_list = view.findViewById(R.id.myPostss);
// textPop = (TextView)view.findViewById(R.id.textMessage);
posts_list = new ArrayList<>();
accept_request = new ArrayList<>();
userRecyclerAdapter = new UserRecyclerAdapter(posts_list, accept_request);
my_list.setLayoutManager(new LinearLayoutManager(container.getContext()));
my_list.setAdapter(userRecyclerAdapter);
firebaseFirestore.collection("Foody_Post").whereEqualTo("u_id",u_id).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for (final DocumentSnapshot d : task.getResult()){
p_id = d.getId();
final String date = d.getString("date");
final String msg = d.getString("message");
final String rs = d.getString("price");
final String pc = d.getString("partner_count");
final String addr = d.getString("address");
Log.d(TAG, "Listt is : " );
firebaseFirestore.collection("Send_req").whereEqualTo("post_id",p_id)
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for (DocumentSnapshot d1 : task.getResult()){
a = d1.getId();
b = d1.getString("sender_id");
accept_request.add(a);
Log.d(TAG, "List2 is : "+a );
}
}
final AllPosts allPosts = d.toObject(AllPosts.class);
posts_list.add(allPosts);
allPosts.setDate(date);
allPosts.setAddress(addr);
allPosts.setMessage(msg);
allPosts.setPrice(rs);
allPosts.setPartner_count(pc);
allPosts.setSend_req_doc_id(a);
allPosts.setSender_id(b);
userRecyclerAdapter.notifyDataSetChanged();
}
});
}
}
}
});
return view;
}}
Класс адаптера:
public class UserRecyclerAdapter extends RecyclerView.Adapter<UserRecyclerAdapter.ViewHolder> {
Context context;
public List<AllPosts> posts_list;
public List<String> accept_request;
private SparseBooleanArray expandState = new SparseBooleanArray();
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth mAuth;
private StorageReference storageReference;
String u_id, s_name, s_gender, s_id;
private AdapterView.OnItemClickListener listener;
RecyclerView send_req_user_list;
List<String> senderList;
String post_id;
public UserRecyclerAdapter(List<AllPosts> posts_list, List<String> accept_request) {
this.posts_list = posts_list;
this.accept_request = accept_request;
//set initial expanded state to false
for (int i = 0; i < posts_list.size(); i++) {
expandState.append(i, false);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mAuth = FirebaseAuth.getInstance();
u_id = mAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_list, parent, false);
context = parent.getContext();
UserRecyclerAdapter.ViewHolder vh = new UserRecyclerAdapter.ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.setIsRecyclable(false);
final String msg_data = posts_list.get(position).getMessage();
holder.setMessageText(posts_list.get(position).getMessage());
final String count_data = posts_list.get(position).getPartner_count();
holder.setCountText(count_data);
final String price = posts_list.get(position).getPrice();
holder.setrsText(price);
final String location_data = posts_list.get(position).getAddress();
holder.setLocationText(location_data);
/*****To set Date*******/
String dateTime = posts_list.get(position).getDate();
Log.d(TAG,"DateTime : "+dateTime);
//To remove substring
String dateOnly = dateTime.split("Time")[0];
Log.d(TAG,"DateTime new : "+dateOnly);
//To remove DATE from string
String dateRemove = dateOnly.replace("Date:","");
Log.d(TAG,"DateRemoved : "+dateRemove);
//To get last two digits of year
String year = dateRemove.substring(dateRemove.length()-3);
Log.d(TAG,"Year :"+year);
//To get last two digits of year
String year2 = dateRemove.substring(0,dateRemove.length()-5);
Log.d(TAG,"Year2 :"+year2);
String finalDate = year2+""+year;
Log.d(TAG,"final :"+finalDate);
holder.setDateText(finalDate);
post_id = posts_list.get(position).getPost_id();
Log.d(TAG,"post id : "+post_id);
final String sender_id = posts_list.get(position).getSender_id();
Log.d(TAG,"sender id : "+sender_id);
final String sender_name = posts_list.get(position).getSender_name();
Log.d(TAG,"sender name : "+sender_name);
final String send_r_d_i = posts_list.get(position).getSend_req_doc_id();
Log.d(TAG,"sender doc_id : "+send_r_d_i);
//check if view is expanded
final boolean isExpanded = expandState.get(position);
// holder.expandedConst.setVisibility(isExpanded?View.VISIBLE:View.GONE);
Log.d(TAG,"Bind Complete food :"+accept_request);
holder.cardView.setTag(position);
//initializing the productlist
}
@Override
public int getItemCount() {
return posts_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View mView;
private TextView rupees, date, location_txt, message, count, gender, name, foodpreferred;
Button sendRequest;
CardView cardView;
ConstraintLayout constraintLayout, expandedConst;
private AdapterView.OnItemClickListener listener;
public ViewHolder( View itemView) {
super(itemView);
mView = itemView;
constraintLayout = mView.findViewById(R.id.u_cont);
cardView = mView.findViewById(R.id.u_cardd);
cardView.setOnClickListener(this);
}
public void setrsText(String rs){
rupees = mView.findViewById(R.id.u_rs);
rupees.setText(rs);
}
public void setDateText(String dt){
date = mView.findViewById(R.id.utime);
date.setText(dt);
}
public void setLocationText(String locationText){
location_txt = mView.findViewById(R.id.ulocation_text);
location_txt.setText(locationText);
}
public void setMessageText(String msg){
message = mView.findViewById(R.id.umsg);
message.setText(msg);
}
public void setCountText(String countText){
count = mView.findViewById(R.id.ucount);
count.setText(countText);
}
@Override
public void onClick(final View view) {
final int position = (int) view.getTag();
Toast.makeText(view.getContext(),Integer.toString(position),Toast.LENGTH_SHORT).show();
}
}}
база данных: