1

I am trying to retrieve data from firebase and creating an android list view from the data retrieved. For example I'd like to retrieve all the messages from https://docs-examples.firebaseio.com/web/data and create an android list view for each message. Where the child's of the messages would simply be data displayed for each single list view.

This https://www.firebase.com/docs/android/guide/retrieving-data.html helps explain the supported Firebase datatypes but still find it difficult to understand. Your help is much appreciated.

Thank You!

Listview.java

public class ListingsActivity extends ListActivity {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Firebase.setAndroidContext(this);

        ActionBar actionBar = getActionBar();
        actionBar.hide();

        Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/data/messages/");

        final Map < String, Object > data;

        final List <? > data2;

        // Attach an listener to read the data at our posts reference
        ref.addListenerForSingleValueEvent(new ValueEventListener() {@Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println("Data: " + snapshot.getValue());
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("Something went wrong!");
            }
        });

        // Binding Array to ListAdapter
        // this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_listings, R.id.label, firebaseData));

        ListView lv = getListView();

        System.out.println("HERE!!!");

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {

                // selected item
                String fproduct_label = ((TextView) view).getText().toString();

                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getApplicationContext(), SingleListView.class);
                // sending data to new activity
                i.putExtra("fproduct_label", fproduct_label);
                startActivity(i);
            }
        });
    }

}

SingleView.java

public class SingleListView extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_view);

        TextView txtProduct = (TextView) findViewById(R.id.product_label);

        Intent i = getIntent();
        // getting attached intent data
        String product = i.getStringExtra("fproduct_label");
        // displaying selected product name
        txtProduct.setText(product);
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dev3r
  • 59
  • 1
  • 7
  • 1
    You will have to do some more legwork yourself before we're going to be able to help you here on StackOverflow. Maybe this is a good next step: https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat – Frank van Puffelen Nov 14 '15 at 23:47
  • Hi Frank. Thanks for the note and link! I've updated the post which shares the code I have at the moment. I have two variables which I'd like to store the Firebase retrieved data. Thanks again! – Dev3r Nov 15 '15 at 00:55
  • If you're having trouble with mapping the `onDataChange` to a ListView, I even more recommend the tutorial I linked before. It uses FirebaseUI which handles that mapping for you. You can either use it, or study its code to see how to do it yourself. – Frank van Puffelen Nov 15 '15 at 01:30
  • Thanks for the tip Frank. I will go through the code that's on the shared link and will try again sometime tonight. I'll let you know if I was able to get it working. I think the FirebaseListAdapter is what I was looking for. :) Thanks for sharing! – Dev3r Nov 15 '15 at 01:38
  • @FrankvanPuffelen - The link is broken now – Shraddheya Shendre Dec 29 '16 at 09:00

0 Answers0