如何使用Firebase列表适配器 [英] How to use Firebase List adapter

查看:154
本文介绍了如何使用Firebase列表适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照这个教程:

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



但在我的复杂应用程序上实现它只是没有我创建了这个简单的MainActivity:

 <$ c 

$ c> 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 =新的Firebase(https://< myURL> ..);
$ b $ 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);
}
});


$ / code $ / pre

现在我有几个问题:

<1>什么触发populateView?我只是不能让它运行

<2>什么应该是什么 android.R.layout.simple_list_item_1 是换成?我尝试创建我自己的列表视图,并用我的 R.id.listView 替换上面的,但没有任何反应..我无法弄清楚这个魔法是如何工作的。



3)即使这个简单的应用程序没有工作..按钮确实添加test123在服务器上的正确位置,但我什么都看不到我的应用程序..什么是错的解决方案

我找到了什么错误,我错过了一个列表视图。
正确的代码:
/ p>

  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 =新的Firebase(https://< myURL> ..);
$ b $ 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);
}
});





$ b $ <$ code> lv.setAdapter
是将适配器关联到我的列表,并触发populateView ..
这基本上是我所有的3个问题的答案..



以下是xml:

 <?xml version = 1.0encoding =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_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context =com.twodwarfs.firebaselistadapter.MainActivity>

$ b< 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/>
$ b $ 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>


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

解决方案

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>

这篇关于如何使用Firebase列表适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆