Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Firebase List adapter

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?

like image 562
Yarden Cohen Avatar asked Jun 02 '16 00:06

Yarden Cohen


2 Answers

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>
like image 87
Yarden Cohen Avatar answered Oct 18 '22 03:10

Yarden Cohen


  1. FirebaseListAdapter.populateView() docs:

Each time the data at the given Firebase location changes, this method will be called for each item that needs to be displayed. The arguments correspond to the mLayout and mModelClass given to 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 customized look for your list items. As also per the docs referenced above:

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

  1. I haven't tried using a FirebaseListAdapter before, but you can check out this sample by Puf, might clear things up. Will check it out also, then see if I can figure out what seems to be wrong.

Cheers!

like image 24
AL. Avatar answered Oct 18 '22 01:10

AL.