如何为Listview实现自定义FirebaseAdapter?

时间:2017-04-12 19:43:14

标签: android firebase firebase-realtime-database firebaseui

我使用自定义适配器来实现listview而不是默认的简单列表视图。现在,我想为其添加Firebase功能,以使用它的实时数据库功能。

我知道实施默认FirebaseAdapter方法的步骤,但如何使用Firebase为listview实现自定义适配器?

简单FirebaseAdapter的代码:

  mRef = new Firebase("https://<myURL>..");
  com.firebase.ui.FirebaseListAdapter<String> myAdapter;
  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);

1 个答案:

答案 0 :(得分:0)

可以这样做:

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


    }


}