gpt4 book ai didi

android - 我需要帮助以将按照我的 'Document' 列表排序的 'Collection' 数据显示到 RecyclerView 中(检索到的数据未按照列表排序,#AskFirebase)

转载 作者:行者123 更新时间:2023-11-29 22:46:36 26 4
gpt4 key购买 nike

Address collection database image

Document list database image

我想根据我的列表将排序后的数据显示到 RecyclerView 中(我可以检索数据,但它不是根据我的集合列表排序的)。或者,我已经在 Firestore 中对地址集合列表进行了排序并想要获取从 Firestore 文档列表中排序的数据。请找到随附的 Firestore 数据库图像和 Java 代码以了解详细信息。

public class ViewSelectedActivity extends AppCompatActivity {

private RecyclerView addressRecyclerView;
private FirebaseFirestore fireStoreDb = FirebaseFirestore.getInstance();
public static AddressAdapter addressActivityAdapter;

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

addressRecyclerView = findViewById(R.id.address_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

addressRecyclerView.setLayoutManager(layoutManager);

final List<AddressModel> addressModelList1 = new ArrayList<>();
addressModelList1.clear();

addressModelList1.add(new AddressModel("Ravi11111", "Fist1", "City1", "111111", "111"));//Manual address list is working fine.
addressModelList1.add(new AddressModel("222", "Fist1", "222", "111111", "111"));
addressModelList1.add(new AddressModel("Ravi11111", "333", "City33", "111111", "111"));

fireStoreDb.collection("Selected_user").document("Address").get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull final Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
for (long x = 1; x <= (long) task.getResult().get("Address_Size"); x++) {
String addressId = task.getResult().getString("Address_0" + x);

fireStoreDb.collection("AddressCollection").document(addressId).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task1) {
if (task1.isSuccessful()) {
addressModelList1
.add(new AddressModel(task1.getResult().getString("Full_Name")
, task1.getResult().getString("House_No")
, task1.getResult().getString("City")
, task1.getResult().getString("Pincode")
, task1.getResult().getString("Mobile_No")));
addressActivityAdapter.notifyDataSetChanged();
}
}
});
}
}
}
});

addressActivityAdapter = new AddressAdapter(addressModelList1);
addressRecyclerView.setAdapter(addressActivityAdapter);
//address1adapter.notifyDataSetChanged();
}
}



public class AddressModel {

private String name;
private String house;
private String city;
private String pincode;
private String mobile;

public AddressModel(String name, String house, String city, String pincode, String mobile) {
this.name = name;
this.house = house;
this.city = city;
this.pincode = pincode;
this.mobile = mobile;
}

public String getName() {
return name;
}

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

public String getHouse() {
return house;
}

public void setHouse(String house) {
this.house = house;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getPincode() {
return pincode;
}

public void setPincode(String pincode) {
this.pincode = pincode;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}
}


public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {

private List<AddressModel> addressModelList;

public AddressAdapter(List<AddressModel> addressModelList) {
this.addressModelList = addressModelList;
}

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

@Override
public void onBindViewHolder(@NonNull AddressViewHolder viewHolder, int position) {

String b_name = addressModelList.get(position).getName();
String b_house = addressModelList.get(position).getHouse();
String b_city = addressModelList.get(position).getCity();
String b_pincode = addressModelList.get(position).getPincode();
String b_mobile = addressModelList.get(position).getMobile();

viewHolder.setData(b_name,b_house,b_city,b_pincode, b_mobile);
}

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

class AddressViewHolder extends RecyclerView.ViewHolder {

private TextView aname;
private TextView ahouse;
private TextView acity;
private TextView apincode;
private TextView amobile;

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

aname = itemView.findViewById(R.id.view_full_name);
ahouse = itemView.findViewById(R.id.view_house_no);
acity = itemView.findViewById(R.id.view_city);
apincode = itemView.findViewById(R.id.view_pincode);
amobile = itemView.findViewById(R.id.view_mobile_no);
}

private void setData(String nameText, String houseText, String cityText, String pinText, String mobileText) {
aname.setText(nameText);
ahouse.setText(houseText);
acity.setText(cityText);
apincode.setText(pinText);
amobile.setText(mobileText);
}
}

}

最佳答案

假设您想要按名称对地址进行排序,在您的 onComplete 回调中,添加一个调用以对列表进行排序:

addressModelList1.add(new AddressModel( ... ));

// Sort list by name:
Collections.sort(addressModelList1, (address1, address2) -> address1.getName().compareTo(address2.getName());

addressActivityAdapter.notifyDataSetChanged();

关于android - 我需要帮助以将按照我的 'Document' 列表排序的 'Collection' 数据显示到 RecyclerView 中(检索到的数据未按照列表排序,#AskFirebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58331535/

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