How to use Firebase List adapter

前端 未结 2 1381
终归单人心
终归单人心 2020-12-18 01:35

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

相关标签:
2条回答
  • 2020-12-18 02:06
    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!

    0 讨论(0)
  • 2020-12-18 02:13

    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>
    
    0 讨论(0)
提交回复
热议问题