How to use Firebase List adapter

Yarden Cohen picture Yarden Cohen · Jun 2, 2016 · Viewed 23.9k times · Source

I'm trying to follow with this tutorial:

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

but implementing it on my complex app 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 triggers the populateView? I just couldn't make it run

2) what exactly should the android.R.layout.simple_list_item_1 be replaced with? I tried creating my own listview and replace the above with my R.id.listView but nothing happens.. I can't figure out how this magic works..

3) even this simple app didn't work.. the button does add the "test123" to the right place on the server but I see nothing on my app.. whats wrong?

Answer

Yarden Cohen picture Yarden Cohen · Jun 2, 2016

I found out what was wrong, I was missing a 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");
            }
        });
    }


}

the lv.setAdapter is what associating the adapter to my list and also triggers the populateView.. That's basically the answer to all of my 3 questions at once..

here is the xml as well:

<?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>