0
public class ShowCustomer extends AppCompatActivity {

FirebaseDatabase database;
DatabaseReference reference;
AddCustomer customer;

private EditText fname;

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

    database = FirebaseDatabase.getInstance();
    reference = database.getReference("Customers");
    customer = new AddCustomer();


    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds: dataSnapshot.getChildren() ) {


                customer = ds.getValue(AddCustomer.class);
                fname = (EditText) findViewById(R.id.fnameTxt);
                fname.setKeyListener(null);
                fname.setText(customer.getFname().toString());


            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



}

}

Question: The customer name is getting displayed on the listview by retrieving them through firebase database. If i click on the items on the listview the details of the customers needs to be shown on another activity. Given bellow is the code segment of the listview onItemClickListner.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(CustomerListView.this, ShowCustomer.class);
                    startActivity(intent);
                }
            });

The fname of the last customer is getting displayed if any item on the listview is tapped. Help me solve this problem. Thanks in advance.

The database structure is given here enter image description here

Coder
  • 338
  • 2
  • 19

1 Answers1

0

To solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference customersRef = rootRef.child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<AddCustomer> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            AddCustomer addCustomer = ds.getValue(AddCustomer.class);
            list.add(addCustomer);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        YourCustomAdapter<AddCustomer> arrayAdapter = new YourCustomAdapter<AddCustomer>(this, list);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
customersRef.addListenerForSingleValueEvent(eventListener);

So you only need to create a ListView in your .XML file and a custom adapter. That's it!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Let me know if you have other questions. – Alex Mamo Mar 29 '18 at 06:05
  • Thank you for your reply. Actually this code segment will output the items on the database on the listview isn't it? but I need have an onclick function where if the customer name on the listview is tapped it should show the details of the customers on labels. – Coder Mar 29 '18 at 19:19
  • Will output the items within the `Customers` node. So the only thing that you need to do is to create a `YourCustomAdapter` class (according to your needs) so when a user clicks on an item, it will get the details of that particular item, right? – Alex Mamo Mar 29 '18 at 20:08
  • can you suggest me some examples on **YourCustomAdapter** ?? Little bit confused on how to use that class – Coder Mar 29 '18 at 22:01
  • If you are interested, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Mar 31 '18 at 09:23
  • Thank you so much :) I will definitely try and let you know – Coder Apr 01 '18 at 14:59