【发布时间】:2019-06-27 07:41:31
【问题描述】:
Refer the image
我想将 favourite_city 的元素与 options 进行匹配,如果找到匹配项,则将 favourite_city 列表设置为 options 中的最上面位置,并带有自己的内容。
第一个数组列表:
favourite_city=["海得拉巴","班加罗尔"]
第二个数组列表:
options=["Nashik","Hyderabad","Mumbai","Bangalore","Chennai","Pune"]
结果:
options=["Hyderabad","Bangalore","Nashik","Hyderabad","Mumbai","Bangalore","Chennai","Pune"]
DocumentReference docRef5 = FirebaseFirestore.getInstance().collection("admin").document("users_profile").collection("dynamic_profile").document("city");
docRef5.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
//-----------code for loading array item from firebase to spinner view-----------------
final List<String> group = (List<String>) document.get("favourite_city");
Log.d(TAG, "favourite city list data: " + group);
final List<String> group1 = (List<String>) document.get("options");
Log.d(TAG, "options list data: " + group1);
cityAdapter = new ArrayAdapter<String>(AdminSetting.this, android.R.layout.simple_spinner_item, group1);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
citySpinner.setAdapter(cityAdapter);
StringBuilder str = new StringBuilder();
for (int i = 0; i < group.size(); i++) {
if (i >= 0)
str = str.append(group.get(i));
String searchedItem = str.toString();
Log.d(TAG, "result " + searchedItem);
int itemPosition = cityAdapter.getPosition(searchedItem);
for(int j=0;j<=i;j++) {
if (itemPosition == -1) {
String message = searchedItem + " : Item not found.";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
} else {
String message = searchedItem + " : Item found and selected.";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
citySpinner.setSelection(itemPosition);
}
}
Log.d(TAG, "after appending " + str);
}
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
【问题讨论】:
-
您想将两个列表或匹配结果合并到新列表中?
-
@Priya 您要求合并两个 ArrayList?
-
首先我想比较两个arrayList,如果找到匹配,那么我想合并选项ArrayList。
标签: android arraylist google-cloud-firestore spinner