实际上,当点击更新按钮时,我想更新地址和描述。 update()无法正常工作的参数是什么?

时间:2017-05-05 14:11:06

标签: firebase

// college_landingpage.java

 private DatabaseReference mref;
    private ImageView college_profile_pic;
    ArrayList<College> colleges;
    private TextView address;
    private TextView description;


    private Button btnupdate;

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

        mref = FirebaseDatabase.getInstance().getReference();
        address = (TextView) findViewById(R.id.address);
        college_profile_pic = (ImageView) findViewById(R.id.college_profile_pic);
        description = (TextView) findViewById(R.id.description);
        btnupdate = (Button) findViewById(R.id.btnupdate);
        colleges=new ArrayList<>();

        mref.child("Colleges").addChildEventListener(new ChildEventListener() {
                                                         @Override
                                                         public void onChildAdded(DataSnapshot snapshot, String s) {
                                                             if (snapshot.hasChildren()) {
                                                                 for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                                                                     College college = postSnapshot.getValue(College.class);
                                                                     String add = "Address :" + college.getAddress() + "\n";
                                                                     String descr = "\n Description" + college.getDescription() + "\n\n";

                                                                     address.setText(add);
                                                                     description.setText(descr);

                                                                 }
                                                             }
                                                         }

                                                         @Override
                                                         public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                                                            String key=dataSnapshot.getKey();
                                                             College newcollege=dataSnapshot.getValue(College.class);
                                                             for(College cl:colleges){
                                                                 if (cl.getKey().equals(key)){
                                                                     cl.setValues(newcollege);
                                                                     break;
                                                                 }
                                                             }

                                                         }

                                                         @Override
                                                         public void onChildRemoved(DataSnapshot dataSnapshot) {

                                                         }

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

                                                         }

                                                         @Override
                                                         public void onCancelled(DatabaseError databaseError) {
                                                             System.out.println("The read failed: " + databaseError.getMessage());
                                                         }
        }



        );



    }
    public void update(College college, String newaddress, String newdescription) {
        college.setAddress(newaddress);
        college.setDescription(newdescription);
        mref.child("colleges").child(college.getKey()).setValue(college);
    }


    @Override
    public void onClick(View v) {
        if (v==btnupdate)

            update(colleges,address,description);
    }
}

//College.java
public class College {
    String name,address,description;
    private String key;

    public College() {

    }

    public String getKey(){
        return key;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setValues(College newcollege) {
        address=newcollege.address;
        description=newcollege.description;
    }
}

// activity_college_landingpage

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_college_landingpage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.learn.binod.navigationdrawer.College_landingpage">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:layout_centerHorizontal="true">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_institution"
        android:layout_alignParentTop="true"
        android:layout_margin="15dp"
        android:layout_marginTop="11dp"
        android:id="@+id/college_profile_pic" />

    <TextView
        android:text="Descrition"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/description"
        android:maxLines="3"
        android:layout_margin="15dp"
        android:layout_marginBottom="95dp"
        android:layout_above="@+id/textView4" />

    <TextView
        android:text="address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="154dp"
        android:layout_margin="15dp"
        android:id="@+id/address"
        android:layout_alignParentBottom="true"
        android:layout_alignEnd="@+id/description"
        android:maxLines="3"
        android:layout_marginEnd="12dp" />

     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/btnupdate"
         android:text="@string/button_update" />
    </LinearLayout>
    </ScrollView>
</RelativeLayout>

我想知道如何在点击btnupdate时执行更新。更新按钮应该能够更新地址和描述 如何进行该活动?

my database structure

1 个答案:

答案 0 :(得分:0)

更新这些孩子的最简单方法是直接在参考上使用setValue()方法。因此,为了实现这一目标,请更改此行:

mref.child("colleges").child(college.getKey()).setValue(college);

mref.child("colleges").child(college.getKey()).child("address").setValue(newAddress);
mref.child("colleges").child(college.getKey()).child("description").setValue(newDescription);
mref.child("colleges").child(college.getKey()).child("name").setValue(newName);

newAddressnewDescriptionnewName是您要更新的新值。

希望它有所帮助。