How to use a Firebase list adapter

I am trying to follow this lesson:

https://www.youtube.com/watch?v=2J6spwAVP0M

but implementing it in my complex application just didn't work, so I tried from scratch ..

I created this simple MainActivity:

public class MainActivity extends AppCompatActivity{ Firebase mRef; com.firebase.ui.FirebaseListAdapter<String> myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRef = new Firebase("https://<myURL>.."); myAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,mRef) { @Override protected void populateView(View view, String s, int i) { TextView text = (TextView)view.findViewById(android.R.id.text1); text.setText(s); } }; Button addBtn = (Button) findViewById(R.id.add_button); addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mRef.push().setValue("test123"); } }); } } 

Now I have a few questions:

1) what causes populateView? I just couldn't get him to run

2) what exactly should android.R.layout.simple_list_item_1 be replaced with? I tried to create my own list view and replace the above with my R.id.listView but nothing happens .. I can’t understand how this magic works ..

3) even this simple application didn’t work ... the button adds "test123" to the right place on the server, but I don’t see anything in my application ... what's wrong?

+7
android listadapter firebase firebase-database firebaseui firebase-realtime-database
source share
2 answers

I found out what happened, I was missing Listview. Heres the corrected code:

 public class MainActivity extends AppCompatActivity { Firebase mRef; com.firebase.ui.FirebaseListAdapter<String> myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRef = new Firebase("https://<myURL>.."); myAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,mRef) { @Override protected void populateView(View view, String s, int i) { TextView text = (TextView) view.findViewById(android.R.id.text1); text.setText(s); } }; final ListView lv = (ListView) findViewById(R.id.listView); lv.setAdapter(myAdapter); Button addBtn = (Button) findViewById(R.id.add_button); addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mRef.push().setValue("test123"); } }); } } 

lv.setAdapter is what connects the adapter to my list and also launches populateView. This is basically the answer to all my 3 questions at once.

here is xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.twodwarfs.firebaselistadapter.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add" android:id="@+id/add_button" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" /> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:layout_toStartOf="@+id/add_button" android:layout_below="@+id/textView" /> </RelativeLayout> 
+6
source share

Each time the data at a given Firebase location changes, this method will be called for each item that should be displayed. The arguments correspond to the mlayout and mModelClass parameters specified by the constructor of this class. Your implementation should populate the view using the data contained in the model.

  1. You can replace it with your own custom layout if you want a custom view of the list items. As in the documents mentioned above:

modelLayout . This is the layout used to represent a single list item. You will be responsible for populating the instance of the corresponding view with data from the modelClass instance.

  1. I have not tried to use FirebaseListAdapter yet, but you can check this sample on Puf , it can clarify the situation. Also check this out and then see if I can figure out what seems wrong.

Hooray!

+3
source share

All Articles