我有一个Firestore数据库,可以在其中正确检索数据,因为我正在打印数组的大小,这是正确的,但是我无法在列表视图中显示它。我坚信这与我的适配器有关。
当我添加NotifyDataSetChanged()方法时,应用程序崩溃了。
我的代码:
public class AdminProducts extends Activity {
private static final String TAG = "AdminProducts";
public ListView myListView;
FirebaseFirestore db;
final ArrayList<String> nameData = new ArrayList<>();
ArrayAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_products);
myListView = (ListView)findViewById(R.id.myProductList);
db = FirebaseFirestore.getInstance();
myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameData);
myListView.setAdapter(myAdapter);
CollectionReference collectionReference = db.collection("myDB");
collectionReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if(!queryDocumentSnapshots.isEmpty()){
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
String name = "xx";
for(DocumentSnapshot d : list){
FirestoreProducts firestoreProducts = d.toObject(FirestoreProducts.class);
name = firestoreProducts.getName();
nameData.add(name);
}
Toast.makeText(AdminProducts.this, name + nameData.size(), Toast.LENGTH_SHORT).show();
((ArrayAdapter)myListView.getAdapter()).notifyDataSetChanged();
}
}
});
}
}
答案 0 :(得分:0)
好的,我已经解决了这个问题。问题在于数组索引之一正在获取空值,这就是为什么它给出NullPointerException错误的原因。我发布的代码可以正常工作。谢谢。