在单个微调器上添加两个 ArrayList
Add two ArrayList on single spinner
Refer the image
我想将 favourite_city 的元素与选项匹配,如果找到匹配项,则将 favourite_city 列表设置为选项中最顶端的位置,并包含其自己的内容。
第一个数组列表:
favourite_city=["Hyderabad","Bangalore"]
第二个数组列表:
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());
}
}
});
首先你的问题不清楚
如果你想合并两个arraylist你需要使用下面的代码
favourite_city.addAll(options) // or Either way
如果你想找到匹配项,你需要像下面那样做
public String getUserId(ArrayList<User> newUsers, ArrayList<User> oldUsers, String email) {
String userId = null;
for (User user1 : newUsers) {
if (user1.email.equals(email)) {
userId = user1.uid;
break;
}
}
if(userId == null){
for (User user2 : oldUsers) {
if (user2.email.equals(email)) {
userId = user2.uid;
break;
}
}
}
return userId;
}
为了将收藏的城市放在第一位,您需要知道它们是否包含在城市列表中。
以这段代码为例:
ArrayList<String> favourites = new ArrayList<>(Arrays.asList("city1", "city2"));
ArrayList<String> cities = new ArrayList<>(Arrays.asList("city3", "city4", "city1", "city2"));
for (String favCity : favourites) {
if (cities.contains(favCity)) {
cities.add(favourites.indexOf(favCity), favCity);
}
}
System.out.println(cities);
这将打印以下内容:
[city1, city2, city3, city4, city1, city2]
如果要删除重复的收藏夹城市以仅将它们保留在第一位,则应在将其添加到列表之前添加此行。
cities.remove(favCity);
它会 return:
[city1, city2, city3, city4]
Refer the image
我想将 favourite_city 的元素与选项匹配,如果找到匹配项,则将 favourite_city 列表设置为选项中最顶端的位置,并包含其自己的内容。
第一个数组列表:
favourite_city=["Hyderabad","Bangalore"]
第二个数组列表:
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());
}
}
});
首先你的问题不清楚
如果你想合并两个arraylist你需要使用下面的代码
favourite_city.addAll(options) // or Either way
如果你想找到匹配项,你需要像下面那样做
public String getUserId(ArrayList<User> newUsers, ArrayList<User> oldUsers, String email) {
String userId = null;
for (User user1 : newUsers) {
if (user1.email.equals(email)) {
userId = user1.uid;
break;
}
}
if(userId == null){
for (User user2 : oldUsers) {
if (user2.email.equals(email)) {
userId = user2.uid;
break;
}
}
}
return userId;
}
为了将收藏的城市放在第一位,您需要知道它们是否包含在城市列表中。
以这段代码为例:
ArrayList<String> favourites = new ArrayList<>(Arrays.asList("city1", "city2"));
ArrayList<String> cities = new ArrayList<>(Arrays.asList("city3", "city4", "city1", "city2"));
for (String favCity : favourites) {
if (cities.contains(favCity)) {
cities.add(favourites.indexOf(favCity), favCity);
}
}
System.out.println(cities);
这将打印以下内容:
[city1, city2, city3, city4, city1, city2]
如果要删除重复的收藏夹城市以仅将它们保留在第一位,则应在将其添加到列表之前添加此行。
cities.remove(favCity);
它会 return:
[city1, city2, city3, city4]