Spinner passing wrong fields to my firestore database android studio java

36 Views Asked by At

I have a firestore document that contain 2 fields : name and link. I populate a spinner with this document.

  • When i populate the name, it pass me the name to my other firestore database.

  • When i populate the link, it pass me the link to my other firestore database.

I want to show the name on the spinner, but when i add a data to other firestore it add the link not the name.

here is the firestore : enter image description here

here is my code :

public class CreateData extends AppCompatActivity {
    EditText League , Team1 , Team2 , Tip ;
    MaterialButton Registerbtn;
    FirebaseFirestore db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_data);
        db = FirebaseFirestore.getInstance();
        League = findViewById(R.id.league);
        Team1 = findViewById(R.id.team1);
        Team2 = findViewById(R.id.team2);
        Tip = findViewById(R.id.tip);
        //Id = findViewById(R.id.idtip);
        Registerbtn = findViewById(R.id.btnRegister);

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        CollectionReference imagesRef = db.collection("images");
        Spinner spinner = (Spinner) findViewById(R.id.spinnercountry);
        List<String> imageList = new ArrayList<>();
        List<String> imagelink = new ArrayList<>();
        ArrayAdapter<String> adaptername = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, imageList);
        adaptername.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adaptername);
        ArrayAdapter<String> adapterlink = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, imagelink);
        adapterlink.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapterlink);
        imagesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String images = document.getString("name");
                        imageList.add(images);
                        String links = document.getString("pic");
                        imagelink.add(links);
                    }
                    adapterlink.notifyDataSetChanged();
                }
            }
        });

        Registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //String id = Id.getText().toString();
                String league = League.getText().toString();
                String team1 = Team1.getText().toString();
                String team2 = Team2.getText().toString();
                String tip = Tip.getText().toString();
                String links = spinner.getSelectedItem().toString();
                Map<String,Object> tips = new HashMap<>();
                //tips.put("Document ID",id);
                tips.put("league",league);
                tips.put("team1",team1);
                tips.put("team2",team2);
                tips.put("tip",tip);
                tips.put("image",links);
                tips.put("Created at", new Timestamp(new Date()));

                db.collection("tips")
                        .add(tips)
                        .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                            @Override
                            public void onSuccess(DocumentReference documentReference) {
                                Toast.makeText(CreateData.this,"Successful",Toast.LENGTH_SHORT).show();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull @NotNull Exception e) {

                        Toast.makeText(CreateData.this,"Failed",Toast.LENGTH_SHORT).show();

                    }
                });
            }
        });
    }
}
0

There are 0 best solutions below