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);
}
}