Firebase Database image我想调用我的问题(根节点)及其孩子(问题编号,即int) 一个接一个地使用下一个按钮。
但问题是firebase中的子名称只允许String。
我试过这个
String mCount =" 0&#34 ;;
然后使用将其转换为int int mIntconverted = Integer.parseInt(mCount);
mIntconverted ++;
然后再将int转换为string mCount = Integer.toString(mIntconverted);
但我的应用崩溃了
// Next Button Code here
mNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mIntConvert=Integer.parseInt(count);
mIntConvert++;
count=Integer.toString(mIntConvert);
}
});
java文件
public class MainActivity extends AppCompatActivity {
String count="0";
int countA=0;
TextView mConditionTextview;
Button mButtonFoggy;
Button mNext;
TextView mQuestiontext;
RadioButton mRadiobutton1;
RadioButton mRadiobutton2;
RadioButton mRadiobutton3;
RadioButton mRadiobutton4;
// firebase database refrences
DatabaseReference mRootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReference mRoot=mRootRef.child("Root");
DatabaseReference mQuestion =mRootRef.child("Question");
DatabaseReference mChild=mQuestion.child(count);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mQuestiontext=(TextView)findViewById(R.id.TextViewQuestion);
mConditionTextview =(TextView)findViewById(R.id.TextViewCondition);
mNext=(Button)findViewById(R.id.buttonNext);
}
@Override
protected void onStart(){
super.onStart();
loadQuestion();
}
// method for loading ques
private void loadQuestion() {
mRoot.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text=dataSnapshot.getValue(String.class);
mConditionTextview.setText(text);
// mQuestiontext.setText(text);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mQuestion.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text=dataSnapshot.getValue(String.class);
// mConditionTextview.setText(text);
mQuestiontext.setText(text);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mChild.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text=dataSnapshot.getValue(String.class);
// mConditionTextview.setText(text);
mQuestiontext.setText(text);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Next Button Code here
mNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mIntConvert=Integer.parseInt(count);
mIntConvert++;
count=Integer.toString(mIntConvert);
}
});
}
答案 0 :(得分:0)
Firebase.setAndroidContext(this);
ref = new Firebase(Constants.FIREBASE_URL);
try {
ref.child("Root/Question/").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e("!)@@>>", dataSnapshot.getKey() + " " + dataSnapshot.getValue() + " " + dataSnapshot.getChildren() + " " + dataSnapshot.getChildrenCount());
ArrayList<String> a = (ArrayList) dataSnapshot.getValue();
Log.e("!_@@", a.get(0) + ""); // use your counter value instead of 0
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
} catch (Exception e) {
e.printStackTrace();
}