我试图从我的firebase数据库中获取单个字符串。虽然我没有看到任何错误,但是当我更改数据时,它没有反映出来。我只想在firebase中在线存储一个字符串,然后再检索它。 我正在为Android应用做这个。这是MainActivity的代码。请告诉我哪里出错了。
package sadboy.firebase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
[public class MainActivity extends AppCompatActivity {][1]
private TextView title;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (TextView)findViewById(R.id.title);
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("hey");
mFirebaseInstance.getReference("new_title").setValue("Realtime Database");
mFirebaseInstance.getReference("new_title").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String appTitle = dataSnapshot.getValue(String.class);
Log.e("Hey", appTitle);
title.setText(appTitle);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Hey", "Failed to read app title value.", error.toException());
}
});
}
}
答案 0 :(得分:2)
尝试使用这种方法:
public class FooTest {
@Test
public void testFinalClass(){
// Instantiate the class under test.
Foo foo = new Foo();
// Instantiate the external dependency
FinalClass realFinalClass = new FinalClass();
// Create mock object for the final class.
FinalClass mockedFinalClass = mock(FinalClass.class);
// Provide stub for mocked object.
when(mockedFinalClass.hello()).thenReturn("1");
// assert
assertEquals("0", foo.executeFinal(realFinalClass));
assertEquals("1", foo.executeFinal(mockedFinalClass));
}
修改强>
我对您的布局进行了细微更改,以便更轻松地测试代码。以下是我所做的更改:
<强> activity_main.xml中强>
我添加了一个EditText和Button。
public class MainActivity extends AppCompatActivity {
private TextView title;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = (TextView) findViewById(R.id.title);
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference();
mFirebaseDatabase.child("new_title").setValue("Realtime Database");
mFirebaseDatabase.child("new_title").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String appTitle = dataSnapshot.getValue().toString();
Log.e("Hey", appTitle);
title.setText(appTitle);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Hey", "Failed to read app title value.", error.toException());
}
});
}
}
<强> MainActivity.java 强>
点击发送按钮后,我从EditText中获取字符串并将其作为<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="sadboy.firebase.MainActivity">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:inputType="text" />
<Button
android:id="@+id/bt_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:text="SEND" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
值存储在您的Firebase数据库中,同时收听new_title
处所做的更改并显示值在TextView。
new_title
结果如下:
答案 1 :(得分:0)
您是否在firebase控制台上确定了数据库规则? 像这样改变它们:
{
"rules": {
".read": true,
".write": true
}
}