如何在Firebase数据库中保存选中的项目

时间:2017-02-17 13:30:46

标签: java android firebase firebase-realtime-database

如何在Firebase数据库中保存列表选中项目? 我甚至不能写一个孩子到我的应用程序。 我读过关于听众的内容,但我不知道如何使用它们。 N.B当我使用我的应用程序下面的代码崩溃,如果我检查任何项目 这是我的CourseAdapter.java

package com.example.android.mycourses;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.TextView;

    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 CourseAdapter extends ArrayAdapter<Course> {
        private ChildEventListener mchildEventListener; //to read from the database
        FirebaseDatabase mfirebasedatabase=FirebaseDatabase.getInstance();//to instantiate the database(this is the access point to the database)
        DatabaseReference mDatabase=mfirebasedatabase.getReference().child("courses");


        public CourseAdapter(Context context, ArrayList<Course> objects) {
            super(context, 0, objects);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            Course course = getItem(position);

            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_message, parent, false);
            }
            final TextView English = (TextView) convertView.findViewById(R.id.messageTextView);

            English.setText(course.getCourseName());

            CheckBox checkbox = (CheckBox)convertView.findViewById(R.id.checkbox );
            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if ( isChecked )
                    {
                        String item=English.getText().toString();
                        // save in database
                        Course cc=new Course(item);
                        mDatabase.push().setValue(cc);
                    }
                    else if(!isChecked){
                        //remove from database
                    }
                }
            });
            mchildEventListener =new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Course c=dataSnapshot.getValue(Course.class);
                    CourseAdapter.this.add(c);


                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            };
            mDatabase.addChildEventListener(mchildEventListener);
            return convertView;
        }
    }

这是日志显示为错误的内容

02-17 16:40:16.936 31195-31206/? E/art: Failed sending reply to debugger: Broken pipe
02-17 16:40:17.254 31195-31195/? E/HAL: load: id=gralloc != hmi->id=gralloc
02-17 16:40:17.350 31195-31251/? E/HAL: load: id=gralloc != hmi->id=gralloc
02-17 16:40:17.355 31195-31251/? E/HAL: load: id=gralloc != hmi->id=gralloc
这意味着什么?我怎样才能解决我的问题

0 个答案:

没有答案