我没有以正确的格式获取Firebase数据

时间:2017-01-10 17:38:03

标签: android firebase firebase-realtime-database

我可能会遗漏一些非常愚蠢的东西。但我没有以正确的格式获取firebase数据。

Person.java

public class Person {

    private String name;
    private String email;
    private String hobby;

    public Person(){

    }


    public Person(String name , String email , String hobby){
        this.name= name;
        this.email = email;
        this.hobby = hobby;

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getHobby() {
        return hobby;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
//    private FirebaseAuth mAuth;
    List<Person> person;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        person = new ArrayList<>();
//        mAuth = FirebaseAuth.getInstance();
        context = this;



        viewPager = (ViewPager) findViewById(R.id.pagerView);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), person, context));


    }


    class MyPagerAdapter extends FragmentPagerAdapter {
        String[] tabsArray;
        private List<Person> personInAdapter = new ArrayList<>();
        private Context context;

//        int icons[] = {R.drawable.white_home, R.drawable.white_heart,
//                R.drawable.white_star, R.drawable.white_heart, R.drawable.white_star};

        public MyPagerAdapter(FragmentManager fm, List<Person> person, Context context) {
            super(fm);
            personInAdapter = person;
            this.context = context;
            tabsArray = getResources().getStringArray(R.array.TABS);
        }


//        @Override
//        public CharSequence getPageTitle(int position) {
//            return tabsArray[position];
//        }

        @Override
        public Fragment getItem(int position) {

            return MyFragment.getInstance(position );
        }

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

MyFragment.java

public class MyFragment extends Fragment {
    private TextView upperText;
    private TextView lowerText;
    List<Person> personInAdapter = new ArrayList<>();
    private Context context;
    private DatabaseReference databaseReference;
    public static MyFragment getInstance(int position) {
        MyFragment myFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        myFragment.setArguments(args);

        return myFragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        upperText = (TextView) view.findViewById(R.id.myFragmentUpperText);
        lowerText = (TextView) view.findViewById(R.id.myFragmentLowerText);
        databaseReference = FirebaseUtil.getBaseRef().child("Person");
        int i = getArguments().getInt("position");
         ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get Post object and use the values to update the UI
                Person person = dataSnapshot.getValue(Person.class);

                Log.e("Data In Person  ", person.toString());
                upperText.setText(person.getName());
                lowerText.setText(person.getEmail());
                // [END_EXCLUDE]
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                // [START_EXCLUDE]

            }
        };
       databaseReference.addValueEventListener(postListener);
//        Bundle bundle = getArguments();
//        if (bundle != null) {
//            textView.setText("The value is" +i);
//        }

        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
}

MyFragment ScreenShot

来自LogCat:

 W/ClassMapper: No setter/field for -Ka45AN7nk3rAqTGfcp3 found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN20cVBNFqbCwUj found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45ANAi__pHJhzwjIa found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN9pBd21aiHbS6i found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN8Q1Y5GHOWRnpP found on class testfirebase.com.testprojectforall.Person
 E/Data In Person: testfirebase.com.testprojectforall.Person@b56a0f3
 W/ClassMapper: No setter/field for -Ka45AN7nk3rAqTGfcp3 found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN20cVBNFqbCwUj found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45ANAi__pHJhzwjIa found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN9pBd21aiHbS6i found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN8Q1Y5GHOWRnpP found on class testfirebase.com.testprojectforall.Person
 E/Data In Person: testfirebase.com.testprojectforall.Person@7dec1b0
 W/ClassMapper: No setter/field for -Ka45AN7nk3rAqTGfcp3 found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN20cVBNFqbCwUj found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45ANAi__pHJhzwjIa found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN9pBd21aiHbS6i found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN8Q1Y5GHOWRnpP found on class testfirebase.com.testprojectforall.Person
 E/Data In Person: testfirebase.com.testprojectforall.Person@6fc4829
 W/ClassMapper: No setter/field for -Ka45AN7nk3rAqTGfcp3 found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN20cVBNFqbCwUj found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45ANAi__pHJhzwjIa found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN9pBd21aiHbS6i found on class testfirebase.com.testprojectforall.Person
 W/ClassMapper: No setter/field for -Ka45AN8Q1Y5GHOWRnpP found on class testfirebase.com.testprojectforall.Person
 E/Data In Person: testfirebase.com.testprojectforall.Person@3e83de5

来自我的firebase控制台的示例:

Database Snapshot from Firebase Console

我收到了文字,但无法设置。我在处理firebase请求时遇到了什么问题?

1 个答案:

答案 0 :(得分:2)

您正在将ValueEventListener附加到数据库中的Person节点:

    ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            Person person = dataSnapshot.getValue(Person.class);

            Log.e("Data In Person  ", person.toString());
            upperText.setText(person.getName());
            lowerText.setText(person.getEmail());
            // [END_EXCLUDE]
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            // [START_EXCLUDE]

        }
    };
   databaseReference.addValueEventListener(postListener);

这意味着将使用onDataChange节点下的所有数据调用Person。所以在你的情况下,这将是多人。

在你onDataChange中,你需要通过循环快照中的子节点来处理这个问题:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot personSnapshot: dataSnapshot.getChildren()) {
        Person person = personSnapshot.getValue(Person.class);
        Log.i("Data In Person  ", person.toString());
    }
}