【发布时间】:2018-03-21 12:44:43
【问题描述】:
我有一个带有 ListAdapters 的复杂 FirebaseDB Datamodell,用于在 Android 的 ListView 中显示选定的对象元素。数据已在 firebase 上。
添加完整对象时出现异常,因为缺少相关的 Android-ListView-Elements ...所以我必须只添加所需的 DB 元素。
我尝试了很多,但结果字符串与 items-ArrayList 不匹配。
日志消息: 致命例外:主要 进程:com.connexibel.proficrmfree,PID:2750 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean java.util.ArrayList.add(java.lang.Object)” 在 com.connexibel.proficrmfree.lists.AccountsList$2.onChildAdded(AccountsList.java:257)
第 257 行是所描述的行(我已为您删除了所有注释行)。
有没有人正确的想法来获得正确的格式???
Android ListView.class:
package com.connexibel.proficrmfree.lists;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.connexibel.proficrmfree.R;
import com.connexibel.proficrmfree.adapters.ListAdapterAccounts;
import com.connexibel.proficrmfree.datamodell.DatamodellAccounts;
import com.connexibel.proficrmfree.detailviews.AccountsDetail;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;
public class AccountsList extends AppCompatActivity {
ArrayList<DatamodellAccounts> items;
ListView listView;
ArrayAdapter arrayAdapter;
private AdView adViewTop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_accounts_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
FirebaseDatabase db = FirebaseDatabase.getInstance();
final DatabaseReference refAccounts = db.getReference("Accounts");
refAccounts.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
DatamodellAccounts dm_accounts = dataSnapshot.getValue(DatamodellAccounts.class);
dm_accounts.setAccount_id(dataSnapshot.getKey());
//this is the critical position!!! -->
items.add(dm_accounts.getAccount_name1(), dm_accounts.getAccount_name2(), dm_accounts.getAccount_adress1(), dm_accounts.getAccount_zipcode(), dm_accounts.getAccount_city(), dm_accounts.getAccount_country(), dm_accounts.getAccount_internet());
listView = (ListView) findViewById(R.id.listViewAccountsPortrait);
arrayAdapter = new ListAdapterAccounts(getApplicationContext(), items);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), AccountsDetail.class);
i.putExtra("account_id", items.get(position).getAccount_id());
startActivity(i);
}
});
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
DatamodellAccounts dm_accounts = dataSnapshot.getValue(DatamodellAccounts.class);
dm_accounts.setAccount_id(dataSnapshot.getKey());
items.add(dm_accounts);
listView = (ListView) findViewById(R.id.listViewAccountsPortrait);
arrayAdapter = new ListAdapterAccounts(getApplicationContext(), items);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), AccountsDetail.class);
i.putExtra("account_id", items.get(position).getAccount_id());
startActivity(i);
}
});
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
DatamodellAccounts dm_accounts = dataSnapshot.getValue(DatamodellAccounts.class);
dm_accounts.setAccount_id(dataSnapshot.getKey());
items.remove(dm_accounts);
listView = (ListView) findViewById(R.id.listViewAccountsPortrait);
arrayAdapter = new ListAdapterAccounts(getApplicationContext(), items);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), AccountsDetail.class);
i.putExtra("account_id", items.get(position).getAccount_id());
startActivity(i);
}
});
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
DatamodellAccounts dm_accounts = dataSnapshot.getValue(DatamodellAccounts.class);
dm_accounts.setAccount_id(dataSnapshot.getKey());
//der bestehenden Liste neu erzeugte Objekte hinzufügen
items.add(dm_accounts);
listView = (ListView) findViewById(R.id.listViewAccountsPortrait);
arrayAdapter = new ListAdapterAccounts(getApplicationContext(), items);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), AccountsDetail.class);
i.putExtra("account_id", items.get(position).getAccount_id());
startActivity(i);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("Daten", databaseError.toString());
}
});
adViewTop = (AdView) findViewById(R.id.adViewTop);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adViewTop.loadAd(adRequest);
}
}
【问题讨论】:
标签: android listview arraylist firebase-realtime-database element