我一直在做很多搜索,我无法弄清楚如何从firebase进行简单的查询。这是我的db的结构:
"地点"的父母是数据库的名称。我正试图获得一个名单,其中包括"所有孩子的位置"以下是对我不起作用的内容:
mFirebase = FirebaseDatabase.getInstance().getReference();
//Create location list
mFirebase.child("locations").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot entry: dataSnapshot.getChildren()) {
Log.d("HERE: ", entry.toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
永远不会运行Log.d方法,因此它永远不会进入forEach循环。有没有更简单的方法从数据库查询,或者我做错了吗?感谢
编辑:更改为addListenerForSingleValueEvent()并在ForEach循环之前添加一行以确保实际调用onDataChange()方法(它是)
mFirebase.child("locations").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Entered: ", "onDataChange");
for(DataSnapshot entry: dataSnapshot.getChildren()) {
Log.d("HERE: ", entry.toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
以下是整个代码:(注意:我正在从SQLite切换到firebase,因此仍有来自SQLite的代码,当Firebase工作时,代码将会消失。
package osu.mobile_apps.ohiostatetourchallenge;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.*;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import database.OsuTourDbSchema.DatabaseHelper;
public class ListActivity extends AppCompatActivity {
private RecyclerView mlocations;
private RecyclerView.Adapter mAdapter;
private List<ListItem> listItems;
private DatabaseHelper mDatabaseHelper = new DatabaseHelper(this);
private DatabaseReference mFirebase;
// Stops the back button
@Override
public void onBackPressed() {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TESTING","ON CREATE() FOR LIST");
setContentView(R.layout.activity_list);
//Get username from log in activity
//String userName = getIntent().getExtras().getString("User");
TextView welcome = (TextView) findViewById(R.id.welcome);
if(welcome != null) {
//welcome.setText("Welcome " + userName + "!");
}
mlocations = (RecyclerView) findViewById(R.id.locationList);
//Every item has fixed size
mlocations.setHasFixedSize(true);
mFirebase = FirebaseDatabase.getInstance().getReference();
//Create location list
mFirebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Before Loop: ", dataSnapshot.toString());
for(DataSnapshot entry: dataSnapshot.getChildren()) {
Log.d("HERE: ", entry.getValue().toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mlocations.setLayoutManager(new LinearLayoutManager(this));
List<Location> locations = mDatabaseHelper.getLocations();
Collections.sort(locations, Location.LocationNameComparator);
listItems = new ArrayList<>();
String description;
String smallDescription;
for(Location entry: locations) {
description = entry.getDescription();
//TODO Check if location is unlocked or not
if (description.length()>110){
smallDescription = description.substring(0,110) + "...";
} else {
smallDescription = description;
}
ListItem item = new ListItem(entry.getName(), smallDescription);
listItems.add(item);
}
mAdapter = new myAdpater(listItems);
mlocations.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_list,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.map:
//Start map activity
return true;
case R.id.about:
//Start about activity
Intent intent = new Intent(ListActivity.this, AboutActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View v){
Intent intent = new Intent(ListActivity.this, InformationActivity.class);
if(v.getId()==R.id.textViewHead){
TextView TestView = (TextView) v.findViewById(R.id.textViewHead);
intent.putExtra("Header", TestView.getText());
}
startActivity(intent);
}
}
答案 0 :(得分:0)
代码中的所有内容都是正确的,但由于您需要更改firebase控制台中的规则,因此无效。
然后转到firebase数据库,然后单击规则并将规则更改为:
{
"rules": {
".read": "true",
".write": "true"
}
}
您将在日志中获得此输出: -
D/HERE:: DataSnapshot { key = 1, value = {description=sadasd, latitude=dssdfds} }
D/HERE:: DataSnapshot { key = 2, value = {description=asdasdas, latitude=randomwords} }
为将来的观看者编辑
Frank van Puffelen评论问题来自引文,在关键节点