Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

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

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:

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...