Home > Mobile >  How I show the other field of selected item in document
How I show the other field of selected item in document

Time:01-29

I am creating a spinner which will show the book title. and want to display the price of the selected book. my Firestore database is as follows:

 Books (collection)
|
|--- book1 (document)
|    |--- Price : "100"
|    |--- Title : "Book-1"
|
|--- book2
|    |--- Price : "200"
|    |--- Title : "Book-2"

When I tried the below codes it displayed the same selected item, Could you please tell me what do I need to add to show the price of the selected book?

 public class MainActivity extends AppCompatActivity {
    FirebaseFirestore db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView priceView = findViewById(R.id.price);

        db =  FirebaseFirestore.getInstance();

        CollectionReference booksRef = db.collection("Books");
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        List<String> booksList = new ArrayList<>();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, booksList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        booksRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>(){
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String bt = document.getString("Title");
                        booksList.add(bt);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                priceView.setText("Price:  "   adapterView.getItemIdAtPosition(position).);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
                                          }
        );

    }

CodePudding user response:

The booksList.add(bt); just adds book title in adapter view's list so getItemAtPosition() will return the book title itself and not the document data. One way would be to store the document data in a separate list as shown below:

public class MainActivity extends AppCompatActivity {
    FirebaseFirestore db;

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

        db = FirebaseFirestore.getInstance();

        Spinner spinner = findViewById(R.id.spinner);
        TextView priceTextView = findViewById(R.id.price);

        // List to store all document's data
        List<QueryDocumentSnapshot> list = new ArrayList<>();

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new ArrayList<>());
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        CollectionReference colRef = db.collection("books");
        colRef.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    adapter.add(document.getString("Title"));
                    list.add(document); // <-- add document to list
                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
            adapter.notifyDataSetChanged();
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, android.view.View view, int position, long id) {
                // Get book data from the list by position
                QueryDocumentSnapshot book = list.get(position);
                priceTextView.setText(String.valueOf(book.get("Price")));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.d(TAG, "Nothing selected");
            }
        });
    }
}
  • Related