简体   繁体   English

在 Android Studio Java 中点击 spinner item 时从 firebase 中检索并显示相应的数据

[英]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) {

        }


    });

    





}

} }

Click to see Firebase Database点击查看Firebase数据库

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.

相关问题 无法在 Android Studio 中从 Firebase 实时数据库检索数据 - Can't retrieve data from Firebase Realtime db in Android Studio Android Firebase - 根据微调器更改来自 firebase 的列表视图数据 - Android Firebase - Change listview data from firebase based on spinner 从 Firebase Java 检索计数数据 - Retrieve count data from Firebase Java 如何使用微调器从 Firebase 中检索列表? - How to Retrieve a List from Firebase using a spinner? 从 Firebase 数据库中检索嵌套数据 android - Retrieve nested Data from Firebase Database android 从 firebase 中检索值,递增并将其作为新记录插入到 firebase Android studio - Retrieve value from firebase, increment and insert it to firebase as a new record Android studio 有没有一种方法可以让用户在应用程序(Android Studio)上输入数据,并将通过数据库(Firebase)中的按钮显示 - Is there a way where the user input the data on the app (Android Studio) and it will be display via on Button from the database(Firebase) JAVA Android 工作室不发送数据到 Firebase 实时数据库 - JAVA Android studio Doesn't send data to Firebase Realtime Database 当我从 Firebase Android Studio 读取或写入数据时,我的签名 APK 崩溃 - My Signed APK Crash when i read or write data from Firebase Android Studio 从 swift 中检索 firebase 数据 - Retrieve firebase data from swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM