我在从Firebase数据库检索数据时遇到了麻烦。 我已经实现了所有必要的依赖项,没有错误由检索到的数据不显示。
我试过这么多youtube视频,实现了必要的依赖关系,应用程序运行顺利,但数据没有被检索。
主要activity代码如下:
public class OrdersActivity extends AppCompatActivity {
ListView mListView;
DatabaseReference dbRef;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private static final String TAG = "OrdersActivity";
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders);
Log.d(TAG, "onCreate: Started");
mListView = (ListView) findViewById(R.id.listView);
dbRef = FirebaseDatabase.getInstance().getReference("Orders");
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item);
mListView.setAdapter(arrayAdapter);
dbRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded (@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String value = dataSnapshot.getValue(Orders.class).toString();
arrayList.add(value);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged (@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved (@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved (@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError) {
}
});
}
}
在此处输入图像说明]1
下面是订单的分类:
public class Orders {
private String emri;
private String data;
private String statusi;
private String imgURL;
public Orders (String emri, String data, String statusi, String imgURL) {
this.emri = emri;
this.data = data;
this.statusi = statusi;
this.imgURL = imgURL;
}
public String getEmri () {
return emri;
}
public void setEmri (String emri) {
this.emri = emri;
}
public String getData () {
return data;
}
public void setData (String data) {
this.data = data;
}
public String getStatusi () {
return statusi;
}
public void setStatusi (String statusi) {
this.statusi = statusi;
}
public String getImgURL () {
return imgURL;
}
public void setImgURL (String imgURL) {
this.imgURL = imgURL;
}
}
Build.Gradle(:app)
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.fiek.projektiioc"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.12'
implementation 'com.firebaseui:firebase-ui-firestore:4.3.1'
implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.google.firebase:firebase-analytics:17.4.3'
implementation 'com.google.firebase:firebase-database:19.3.1'
implementation 'com.google.firebase:firebase-messaging:20.2.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
这是Build.Gradle(PROJECT)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.google.gms:google-services:4.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第一个修复:
您正在将ToString()
用于从FireBase的ChildEventListener
回调OnChildAdded()
接收的Orders
对象,同时不重写Orders
类的ToString()
; 因此它调用超类的toString()
。
String value = dataSnapshot.getValue(Orders.class).toString();
因此需要重写orders
的toString()
方法,以返回需要在ListView
行项上显示的内容。
第二个修正:
您没有将ArrayList
附加到ArrayAdapter
,因此需要替换
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item);
与
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, arrayList);