2

In the main activity, there's a listview which shows all the objects pushed to the database using displayAllItems() method:

private void displayAllItems(){
        ListView listOfItems = (ListView) findViewById(R.id.list_of_items);
        FirebaseListAdapter<Item> adapter = new FirebaseListAdapter<Item>(this, Item.class, R.id.list_item, FirebaseDatabase.getInstance().getReference()) {
            @Override
            protected void populateView(View v, Item model, int position) {
                TextView type, subtype, name;
                type = v.findViewById(R.id.item_type);
                subtype = v.findViewById(R.id.item_subtype);
                name = v.findViewById(R.id.item_name);

                type.setText(model.getType());
                subtype.setText(model.getSubtype());
                name.setText(model.getName());
            }
        };
        listOfItems.setAdapter((adapter));
    }

The listview only shows info from 3 fields of Item class objects. How do I get additional information about an item by clicking on it in listview, so I can use this data in a new activity?

8
  • have you checked the answer? Commented Jun 1, 2020 at 19:55
  • to test the solution, I should populate my data to listview. The method I've put in the question is broken Commented Jun 1, 2020 at 20:27
  • Why is it broken Commented Jun 1, 2020 at 20:41
  • I've just fixed it. Going to implement your solution right now Commented Jun 1, 2020 at 21:29
  • But there's one more question: how do I change object's fields in the database? Commented Jun 1, 2020 at 21:32

1 Answer 1

1

Use setOnItemClickListener to click on each item in the listView:

listOfItems.setOnItemClickListener(new OnItemClickListener(){   
    @Override
    public void onItemClick(AdapterView<?>adapter,View v, int position){
        Item item = (Item) adapter.getItemAtPosition(position);

        Intent intent = new Intent(CurrentActivity.this, NewActivity.class);    
        intent.putExtra("type",item.getType());
        startActivity(intent);
    }
});

getItemAtPosition() will get data associated with the specified position in the list. Then use the getters of your class to get the data and send it to the next activity using intent

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.