如何使用 Firebase 列表适配器 [英] 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..
我创建了这个简单的 MainActivity:
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");
}
});
}
}
现在我有几个问题:
1) 是什么触发了 populateView?我只是无法让它运行
1) what triggers the populateView? I just couldn't make it run
2) android.R.layout.simple_list_item_1
究竟应该用什么替换?我尝试创建自己的列表视图并用我的 R.id.listView
替换上面的内容,但没有任何反应.. 我不知道这个魔法是如何工作的..
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) 即使这个简单的应用程序也不起作用..该按钮确实将test123"添加到服务器上的正确位置,但我在我的应用程序上看不到任何内容..怎么了?
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?
推荐答案
我发现出了什么问题,我缺少一个 Listview..这是更正后的代码:
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");
}
});
}
}
lv.setAdapter
是将适配器关联到我的列表并触发 populateView 的东西.这基本上是对我所有 3 个问题的一次回答..
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..
这里也是xml:
<?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屋!