将数据从firebase传递到另一个活动

时间:2017-10-01 14:51:33

标签: android firebase

我在从firebase检索数据时遇到问题。实际上有两个布局,其中第一个布局称为activity_a.xml,这是用于向firebase输入数据,这很好用。第二种布局称为lyt_fragment_a.xml,用于从firebase中检索数据。

我使用带有名为UploadSchedule.java的java文件的layout activity_a.xml成功将数据上传到firebase。但我无法传递我上传的数据,我希望它在lyt_fragment_a.xml上显示

我希望按钮具有可以通过的附加功能。目前它只能将数据上传到有效的firebase。

lyt_fragment_a.xml。

我真的希望得到编码工作,我非常感谢你的帮助。谢谢

fragmentA.java

public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.lyt_fragment_a, container, false);

    uploadsched=(FloatingActionButton) v.findViewById(R.id.fbUploadSched);
    uploadsched.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(fragmentA.this.getActivity(), UploadSchedule.class);
            startActivity(i);
        }
    });

    return v;

} }

UploadSchedule.java

public class UploadSchedule extends AppCompatActivity {
EditText subjectA, contentA;
FloatingActionButton fbSend, fbRefresh;
FirebaseDatabase database;
DatabaseReference ref;
FireBaseHelperA helperA;
String descSubject, descContent;
TextView tsubject, tcontent;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a);
    subjectA = (EditText) findViewById(R.id.SubjectA);
    contentA = (EditText) findViewById(R.id.ContentA);


    fbSend = (FloatingActionButton) findViewById(R.id.fbSend);
    fbRefresh = (FloatingActionButton) findViewById(R.id.fbRefresh);

    database = FirebaseDatabase.getInstance();
    ref = database.getReference("monday");

    fbSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String descSubject = subjectA.getText().toString();
            String descContent = contentA.getText().toString();
            String userId = ref.push().getKey();

            schedItemA monday =  new schedItemA();

            monday.setSubjTitle(descSubject);
            monday.setSubjContent(descContent);
            ref.child(userId).setValue(monday);

            subjectA.setText("");
            contentA.setText("");



        }
    });

    fbRefresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ref.child("monday").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    schedItemA monday = dataSnapshot.getValue(schedItemA.class);
                    tsubject.setText((CharSequence) monday);
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
    });

}}

lyt_fragment_a.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.testgram.testgram.Tab">

<TextView
    android:id="@+id/dispSubjectA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Subject: "
    android:textSize="22sp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"/>

<TextView
    android:id="@+id/showSubjectA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_toRightOf="@+id/dispSubjectA"/>

<TextView
    android:id="@+id/dispContentA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="What is in our class? "
    android:textSize="22sp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/dispSubjectA"/>

<TextView
    android:id="@+id/showContentA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textSize="22sp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:lines="13"
    android:width="345dp"
    android:layout_below="@+id/dispContentA"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fbUploadSched"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="32dp"
    android:clickable="true"
    app:backgroundTint="@android:color/holo_blue_dark"
    app:fabSize="mini"
    app:srcCompat="@android:drawable/ic_menu_upload"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="32dp" />

activity_a.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.testgram.testgram.UploadSchedule">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fbSend"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="30dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp"
    android:layout_weight="0.51"
    android:clickable="true"
    app:fabSize="mini"
    app:srcCompat="@android:drawable/ic_menu_upload" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fbRefresh"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:clickable="true"
    app:backgroundTint="@android:color/holo_orange_dark"
    app:fabSize="mini"
    app:srcCompat="@android:drawable/ic_menu_revert" />

<TextView
    android:id="@+id/dispSubjectA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Subject: "
    android:textSize="22sp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"/>

<EditText
    android:id="@+id/SubjectA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:layout_marginTop="9dp"
    android:layout_toRightOf="@+id/dispSubjectA"
    android:hint="Enter your subject"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="6dp" />

<TextView
    android:id="@+id/dispContentA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="What is in our class? "
    android:textSize="22sp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/dispSubjectA"/>

<EditText
    android:id="@+id/ContentA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:lines="14"
    android:width="345dp"
    android:hint="Describe what will be going held in this day"
    android:inputType="textPersonName"
    android:layout_below="@+id/dispContentA"
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="6dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="23dp"
    android:layout_below="@+id/ContentA"
    android:id="@+id/tsubject" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="23dp"
    android:layout_below="@+id/tsubject"
    android:id="@+id/tcontent" />

1 个答案:

答案 0 :(得分:0)

当片段运行时你需要做的就是你必须请求数据..你没有做。使用onStart方法,这样每当片段在屏幕上可见时,它就会请求数据。

public class MyBloodGroupRequestsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
''
''
''
}
@Override
public void onStart() {
    super.onStart();
   database = FirebaseDatabase.getInstance();
  ref = database.getReference("monday");

  ref.child("monday").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //U did this erliar u know what to do
                //schedItemA monday = dataSnapshot.getValue(schedItemA.class);
                //tsubject.setText((CharSequence) monday);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

}
}