Populating a Spinner with keys from a Firebase database, then another Spinner from the first's selection

↘锁芯ラ 提交于 2019-12-13 23:53:14

问题


I want to get all of the "Group No" keys in a single Spinner, then all of the "School No" keys of the selected "Group No" in a second Spinner.

Here's my database:

Here's my Java code:

public class Collection extends AppCompatActivity {

    TextView ttl, tgat, tsch;
    Button btnshw, btngt;
    Spinner sping;
    DatabaseReference d2ref;

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

        tgat = (TextView) findViewById(R.id.texigat);
        tsch = (TextView) findViewById(R.id.texisch);
        //tcls = (TextView) findViewById(R.id.texiclass);
        ttl = (TextView) findViewById(R.id.texirs);
        btnshw = (Button) findViewById(R.id.bshow);
        btngt = (Button) findViewById(R.id.bgat);
        sping = (Spinner) findViewById(R.id.spingat);

        btnshw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                d2ref = FirebaseDatabase.getInstance().getReference().child("2018-19").child("Group No 14")
                        .child("School no 109").child("Standard 2nd");
                d2ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        String rs = dataSnapshot.child("Rupees").getValue().toString();
                        ttl.setText(rs);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
        });

        btngt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        d2ref = FirebaseDatabase.getInstance().getReference().child("2018-19");
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                final List<String> grpno = new ArrayList<String>();

                for (DataSnapshot dsnap : dataSnapshot.child("Group No 14").getChildren()) {
                    String grp = dsnap.getKey();
                    grpno.add(grp);
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Collection.this, android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                sping.setAdapter(adapter);

                /*I wasn't able to get the value in 1st spinner so I didn't
                 wrote program for second spinner*/
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
        d2ref.addListenerForSingleValueEvent(eventListener);
            }
        });
    }
}

I've tried to fetch it in the TextView and increment the value with the Button, but the TextView gets only "Group No 2" in it.


回答1:


Putting the key into a Spinner is as simple as putting the value into the Spinner. All you have to do is to remove the child() from the for loop.

Instead of doing this:

for (DataSnapshot dsnap : dataSnapshot.child("Group No 14").getChildren()) {
    String grp = dsnap.getKey();
    grpno.add(grp);
}

You have to do this:

for (DataSnapshot dsnap : dataSnapshot.getChildren()) {
    String grp = dsnap.getKey();
    grpno.add(grp);
}

Hope this helps..



来源:https://stackoverflow.com/questions/55765225/populating-a-spinner-with-keys-from-a-firebase-database-then-another-spinner-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!