如何将firebase数据库中的名称节点添加到listview中

时间:2017-11-05 16:03:49

标签: android listview firebase firebase-realtime-database

这是我的数据库图片 enter image description here

我正在尝试将名称节点添加到列表视图中,如何将单个firebase节点添加到listview中?我尝试了几件事,但无法做到。我的catlog说

  

“引起:java.lang.NullPointerException:尝试调用虚拟   方法'com.google.firebase.database.DatabaseReference   com.google.firebase.database.FirebaseDatabase.getReference(java.lang.String中)”   在空对象引用“

请有人告诉我如何做到这一点我是firebase的新手。

public class Technician extends Activity {
private FirebaseDatabase database;

ListView listView;
ArrayList<String> tecnames = new ArrayList<>();
private FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_technician);

    ImageButton Back_Btn = (ImageButton) findViewById(R.id.back);
    Back_Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Technician.this, Technician_main.class);
            startActivity(i);
            finish();
        }
    });

    DatabaseReference myRef = database.getReference("user");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                String value = childSnapshot.getValue(String.class);
                tecnames.add(value);
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });

    listView = (ListView) findViewById(R.id.list_item);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


            Intent intent = new Intent(Technician.this, Book_Now.class);
            startActivity(intent);
            finish();
        }
    });

    Technician.CostumAdapter costumAdapter = new Technician.CostumAdapter();
    listView.setAdapter(costumAdapter);

}

private class CostumAdapter extends BaseAdapter {


    @Override
    public int getCount() {
        return tecnames.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.cuctom_listview, null);

        TextView txt_one = (TextView) view.findViewById(R.id.skill_txt);
        txt_one.setText(tecnames.get(i));
        return view;
    }

}
}

1 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("user");

null

您收到<span class="label-value">{{is-empty-value (convert-to-currency card.balances.current-balance)}}</span> ,因为您没有实例化您的参考资料。