[英]Retrieve and display corresponding data from firebase when clicking on spinner item in Android Studio Java
I'm making an application that pulls all the information about a lake when selected.我正在制作一个应用程序,在选择时提取有关湖泊的所有信息。 I have a lake database in firebase with all the important information like name, location, hours, fish found, etc. I have populated a spinner with the names of all the lakes.
我在 firebase 中有一个湖泊数据库,其中包含所有重要信息,例如名称、位置、时间、发现的鱼等。我已经用所有湖泊的名称填充了一个微调器。 Now I want to click on a specific lake name from the spinner to get all of the information displayed below the spinner, I have done a ton of research but nothing helps.
现在我想从微调器中单击一个特定的湖泊名称以获取微调器下方显示的所有信息,我已经进行了大量研究但没有任何帮助。
This is the code I have so far这是我到目前为止的代码
public class places_to_fish extends AppCompatActivity {
Spinner spinner;
DatabaseReference databaseReference;
List<String> names;
String selectedName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places_to_fish);
names = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Places").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot childSnap : snapshot.getChildren()) {
String spinnerName = childSnap.child("Name").getValue(String.class);
names.add(spinnerName);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(places_to_fish.this, android.R.layout.simple_spinner_item, names);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedName = arrayAdapter.getItem(i);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
} }
create class Lake.java创建 class Lake.java
public class Lake {
public String Fish, Hours, Location, Name, Zipcode ;
public Lake(){}
public Lake(String Fish, String Hours,String Location, String Name, String Zipcode){
this.Fish = Fish;
this.Hours = Hours;
this.Location = Location;
this.Name = Name;
this.Zipcode = Zipcode;
}
}
then, in your current class declare a HashMap然后,在您当前的 class 中声明一个 HashMap
HashMap<String, Lake> map = new HashMap<String, Lake>();
now, initialize the map and add all the lakes into it现在,初始化 map 并将所有湖泊添加到其中
map.clear();
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Iterator <DataSnapshot> it = snapshot.getChildren().iterator();
while(it.hasNext() ){
DataSnapshot item = it.next();
name = item.child("full_name").getValue().toString().trim();
String Fish = item.child("Fish").getValue().toString().trim();
String Hours = item.child("Hours").getValue().toString().trim();
String Location = item.child("Location").getValue().toString().trim();
String Name = item.child("Name").getValue().toString().trim();
String Zipcode = item.child("Zipcode").getValue().toString().trim();
Lake tmp_Lake = new Lake(Fish,Hours,Location,Name,Zipcode);
map.put(Name,tmp_Lake);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
now you have a map with all the lakes info (sorted by their names) so now, when you choose lake, you can take all the details from the map. this code will create array "names" with the lake names现在你有一个 map,其中包含所有湖泊信息(按名称排序)所以现在,当你选择湖泊时,你可以从 map 中获取所有详细信息。此代码将创建包含湖泊名称的数组“名称”
public void addToList(){
String [] names = new String[map.size()];
int i = 0;
for(User tmp : map.values()){
names[i] = tmp.Name;
i++;
}
call this method after you initialize the map and finally, displaying the selected lake info by creating a string and load it to a textbox在初始化 map 之后调用此方法,最后,通过创建一个字符串并将其加载到文本框来显示选定的湖泊信息
ArrayAdapter arrayAdapter = new ArrayAdapter(place_to_fish.this, android.R.layout.simple_list_item_1,names);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
String lake_name = names[i];
Lake tmp = map.get(names[i]);
String [] Lake_details = new String[5];
Lake_details[0] = "Name: "+tmp.Name + "\n";
Lake_details[1] = "Fish: "+tmp.Fish + "\n";
Lake_details[2] = "Location: "+tmp.Location + "\n";
Lake_details[3] = "Hours: "+tmp.Hours + "\n";
Lake_details[4] = "Zipcode "+tmp.Zipcode + "\n";
String text = "";
for (int i = 1; i < Lake_details.length; i++)
text += Lake_details[i];
full_text.setText(text);//"full_text" is a clear text box
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.