如何在textView中获取firebase数据库

时间:2017-05-04 10:06:43

标签: android firebase firebase-realtime-database

我想在textview中显示我的数据

    package com.example.u_ahm.firebase;
enter code hereimport android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
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 com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private DatabaseReference mDatabase;
    EditText etID, etName, etAddress, etSeats, etNumber;
    Button addbtn,delbtn ,upbtn , showbtn;
    TextView txt;
    int id = 1;

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

        mDatabase = FirebaseDatabase.getInstance().getReference().child("College");

        init();

    }

    private void init() {

        etID = (EditText) findViewById(R.id.idText);
        etName = (EditText) findViewById(R.id.nameText);
        etAddress = (EditText) findViewById(R.id.addText);
        etSeats = (EditText) findViewById(R.id.seatsText);
        etNumber = (EditText) findViewById(R.id.numText);


        addbtn  = (Button) findViewById(R.id.insertBtn);
        delbtn = (Button) findViewById(R.id.delBtn);
        upbtn = (Button) findViewById(R.id.updateBtn);
        showbtn = (Button) findViewById(R.id.showBtn);

        addbtn.setOnClickListener(dbButtonsListener);
        delbtn.setOnClickListener(dbButtonsListener);
        upbtn.setOnClickListener(dbButtonsListener);
        showbtn.setOnClickListener(dbButtonsListener);

        txt = (TextView) findViewById(R.id.textView2);
    }

    private View.OnClickListener dbButtonsListener = new View.OnClickListener() {
        @Override
        public void onClick(final View v) {

            switch (v.getId()){
                case R.id.insertBtn:

                    String name = etName.getText().toString().trim();
                    String address = etAddress.getText().toString().trim();
                    String seats = etSeats.getText().toString().trim();
                    String number = etNumber.getText().toString().trim();

                    HashMap<String,String> map = new HashMap<String, String>();

                    map.put("Name",name);
                    map.put("Address",address);
                    map.put("Seats",seats);
                    map.put("Numbers",number);



                    mDatabase.child(Integer.toString(id)).setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isComplete()){
                                Toast.makeText(MainActivity.this,"Data inserted",Toast.LENGTH_SHORT).show();
                                id++;
                            }
                        }
                    });

                    break;
                case R.id.delBtn:

                    break;
                case R.id.updateBtn:

                    break;
                case R.id.showBtn:

                    mDatabase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            for(DataSnapshot snapshot : dataSnapshot.getChildren() ){

                                College college = snapshot.getValue(College.class);

                                String string = txt.getText() + "\nName: " + college.getName()+ "\n Address : " + college.getAddress() +
                                        "\n No. of Seats: " + college.getSeats() + "\n Numbers: " + college.getNumbers();
                                txt.setText(string);

                            }


                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });



                    break;
            }

        }
    };
}

这是我的数据库

    College
 1
 Address: 
"Gulshan e Iqbal"
 Name: 
"Ideal"
 Numbers: 
"560"
 Seats: 
"150"
 2
 Address: 
"Gulshan e Iqbal"
 Name: 
"Ideal"
 Numbers: 
"560"
 Seats: 
"150"
 3
 Address: 
"Gulshan e Iqbal"
 Name: 
"Ideal"
 Numbers: 
"560"
 Seats: 
"150"

目前我收到此错误 com.google.firebase.database.DatabaseException:在反序列化时期望一个Map,但得到一个类java.util.ArrayList

0 个答案:

没有答案