Firebase 实际上,当点击更新按钮时,我想更新地址和描述。update()不起作用要放置哪些参数?
//college_landingpage.javaFirebase 实际上,当点击更新按钮时,我想更新地址和描述。update()不起作用要放置哪些参数?,firebase,Firebase,//college_landingpage.java private DatabaseReference mref; private ImageView college_profile_pic; ArrayList<College> colleges; private TextView address; private TextView description; private Button btnupdate; @Overri
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;
}
}
私有数据库参考mref;
私立ImageView学院\u简介\u图片;
ArrayList学院;
私有文本视图地址;
私有文本视图描述;
私人按钮btnupdate;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u college\u landingpage);
mref=FirebaseDatabase.getInstance().getReference();
地址=(TextView)findViewById(R.id.address);
学院简介图片=(ImageView)findViewById(R.id.college简介图片);
description=(TextView)findViewById(R.id.description);
btnupdate=(按钮)findViewById(R.id.btnupdate);
学院=新阵列列表();
mref.child(“学院”).addChildEventListener(新的ChildEventListener()){
@凌驾
公共void onchildaded(DataSnapshot快照,字符串s){
if(snapshot.haschilds()){
对于(DataSnapshot postSnapshot:snapshot.getChildren()){
学院=postSnapshot.getValue(学院.班级);
String add=“地址:”+college.getAddress()+“\n”;
字符串descr=“\n Description”+college.getDescription()+”\n\n”;
地址.setText(add);
description.setText(descr);
}
}
}
@凌驾
公共void onChildChanged(DataSnapshot DataSnapshot,字符串s){
String key=dataSnapshot.getKey();
College newcollege=dataSnapshot.getValue(College.class);
对于(学院cl:学院){
if(cl.getKey().equals(key)){
cl.setValues(新学院);
打破
}
}
}
@凌驾
ChildRemoved上的公共void(DataSnapshot DataSnapshot){
}
@凌驾
已移动ChildMoved上的公共void(DataSnapshot DataSnapshot,字符串s){
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
System.out.println(“读取失败:+databaseError.getMessage());
}
}
);
}
公共无效更新(学院、字符串newaddress、字符串newdescription){
学院地址(新地址);
学院。设置描述(新描述);
mref.child(“学院”).child(college.getKey()).setValue(学院);
}
@凌驾
公共void onClick(视图v){
如果(v==btnupdate)
更新(学院、地址、说明);
}
}
//College.java
公立学院{
字符串名称、地址、描述;
私钥;
公立学院(){
}
公共字符串getKey(){
返回键;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共价值观(新学院){
地址=newcollege.address;
description=newcollege.description;
}
}
//活动\大学\登陆页面
<?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时如何执行更新。更新按钮应该能够更新地址和说明
如何执行该活动
更新这些子项的最简单方法是直接在引用上使用
setValue()
方法。因此,为了实现这一点,请更改这一行:
mref.child("colleges").child(college.getKey()).setValue(college);
与
其中,newAddress
、newDescription
和newName
是要更新的新值
希望有帮助。先生,我需要知道update()方法中的参数是什么,最后我需要输入什么@重写public void onClick(View v){if(v==btnupdate)update(学院、地址、描述)
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);