获取Firebase数据的ID。

huangapple go评论142阅读模式
英文:

Get the id of firebase data

问题

使用ListView来显示存储在Firebase数据库中的数据,当我长按列表项时,我想要获取Firebase中列表项源的自动生成的ID,以便能够通过获取ID来更新或删除数据。

patientListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Patient p = patientList.get(position);
        String Id = p.getId();
        mDatabaseRef.child(Id).removeValue();
        return false;
    }
});

UPDATE:我使用以下代码将数据添加到数据库中

try {
    String name = mNameEditText.getText().toString().trim();
    String phone = mPhoneEditText.getText().toString().trim();
    String age = mAgeEditText.getText().toString().trim();
    String aptDate = date();
    String visitType = radioListener();

    String gender = mGenderSpinner.getSelectedItem().toString();
    String descCondition = mDescCondEditText.getText().toString().trim();

    if (name.isEmpty()) {
        mNameEditText.setError("Enter Your Name Please!");
        mNameEditText.requestFocus();
    } else if (phone.isEmpty()) {
        mPhoneEditText.setError("Enter Your Phone");
        mPhoneEditText.requestFocus();
    } else if (age.isEmpty()) {
        mAgeEditText.setError("Enter Your Age");
        mAgeEditText.requestFocus();
    } else if (aptDate.isEmpty()) {
        dateEditText.setError("Please Enter Appointment Date");
        dateEditText.requestFocus();
    } else {
        Patient appointment = new Patient(name, phone, age, descCondition, gender, aptDate, visitType);
        mDatabaseRef.push().setValue(appointment);
        successAlertDialog();
        mNameEditText.setText("");
        mPhoneEditText.setText("");
        mAgeEditText.setText("");
        dateEditText.setText("");
        mGenderSpinner.setSelection(0);
        mDescCondEditText.setText("");
    }
}

这是我的模块类:

package com.sharhospital;

public class Patient {
    // 类的属性和方法在这里...
}

更改后的onItemLongClickListener

patientListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Patient p = patientList.get(position);
        String Id = p.getId();
        mDatabaseRef.child(Id).removeValue();
        return false;
    }
});

将数据发送到数据库:

String id = mDatabaseRef.push().getKey();
Patient appointment = new Patient(id, name, phone, age, descCondition, gender, aptDate, visitType);
mDatabaseRef.child(id).setValue(appointment);

图片链接:获取Firebase数据的ID。

英文:

I use a ListView to show the data stored in Firebase database and when I long click on a list item I want to get the auto-generated Id of the list item source in Firebase to be able to update or delete data by getting the Id.

patientListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
Patient p = patientList.get(position);
return false;
}
});

UPDATE: I use this to add data to my database

try {
String name = mNameEditText.getText().toString().trim();
String phone = mPhoneEditText.getText().toString().trim();
String age = mAgeEditText.getText().toString().trim();
String aptDate = date();
String visitType = radioListener();
String gender = mGenderSpinner.getSelectedItem().toString();
String descCondition = mDescCondEditText.getText().toString().trim();
if (name.isEmpty()) {
mNameEditText.setError(&quot;Enter Your Name Please!&quot;);
mNameEditText.requestFocus();
} else if (phone.isEmpty()) {
mPhoneEditText.setError(&quot;Enter Your Phone&quot;);
mPhoneEditText.requestFocus();
} else if (age.isEmpty()) {
mAgeEditText.setError(&quot;Enter Your Age&quot;);
mAgeEditText.requestFocus();
} else if (aptDate.isEmpty()) {
dateEditText.setError(&quot;Please Enter Appointment Date&quot;);
dateEditText.requestFocus();
} else {
Patient appointment = new Patient(name, phone, age, descCondition, gender, aptDate, visitType);
mDatabaseRef.push().setValue(appointment);
successAlertDialog();
mNameEditText.setText(&quot;&quot;);
mPhoneEditText.setText(&quot;&quot;);
mAgeEditText.setText(&quot;&quot;);
dateEditText.setText(&quot;&quot;);
mGenderSpinner.setSelection(0);
mDescCondEditText.setText(&quot;&quot;);
}

and this is my module class
package com.sharhospital;

public class Patient {
private String name;
private String phone;
private String age;
private String describeCond;
private String gender;
private String date;
private String typeOfVisit;
private String docName;
public Patient(String name, String phone, String age, String describeCond, String gender, String date, String typeOfVisit) {
this.name = name;
this.phone = phone;
this.age = age;
this.describeCond = describeCond;
this.gender = gender;
this.date = date;
this.typeOfVisit = typeOfVisit;
}
public Patient(String name, String phone, String age, String describeCond, String gender, String date, String typeOfVisit, String docName) {
this.name = name;
this.phone = phone;
this.age = age;
this.describeCond = describeCond;
this.gender = gender;
this.date = date;
this.typeOfVisit = typeOfVisit;
this.docName = docName;
}
public Patient() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDescribeCond() {
return describeCond;
}
public void setDescribeCond(String describeCond) {
this.describeCond = describeCond;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTypeOfVisit() {
return typeOfVisit;
}
public void setTypeOfVisit(String typeOfVisit) {
this.typeOfVisit = typeOfVisit;
}
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
}

onItemLongListerner after changes

patientListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
Patient p = patientList.get(position);
String Id = p.getId();
mDatabaseRef.child(Id).removeValue();
return false;
}
});

sending data to database

 String id = mDatabaseRef.push().getKey();
Patient appointment = new Patient(id,name, phone, age, descCondition, gender, aptDate, visitType);
mDatabaseRef.child(id).setValue(appointment);

获取Firebase数据的ID。

答案1

得分: 0

要解决这个问题,请将您的类更改为:

public class Patient {
    private String id;
    private String name;
    private String phone;
    private String age;
    private String describeCond;
    private String gender;
    private String date;
    private String typeOfVisit;
    private String docName;

    public Patient(String id, String name, String phone, String age, String describeCond, String gender, String date, String typeOfVisit) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.age = age;
        this.describeCond = describeCond;
        this.gender = gender;
        this.date = date;
        this.typeOfVisit = typeOfVisit;
    }

    public Patient(String id, String name, String phone, String age, String describeCond, String gender, String date, String typeOfVisit, String docName) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.age = age;
        this.describeCond = describeCond;
        this.gender = gender;
        this.date = date;
        this.typeOfVisit = typeOfVisit;
        this.docName = docName;
    }

    public Patient() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDescribeCond() {
        return describeCond;
    }

    public void setDescribeCond(String describeCond) {
        this.describeCond = describeCond;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTypeOfVisit() {
        return typeOfVisit;
    }

    public void setTypeOfVisit(String typeOfVisit) {
        this.typeOfVisit = typeOfVisit;
    }

    public String getDocName() {
        return docName;
    }

    public void setDocName(String docName) {
        this.docName = docName;
    }
}

将数据添加到数据库时,请使用以下代码行:

String id = mDatabaseRef.push().getKey();
Patient appointment = new Patient(id, name, phone, age, descCondition, gender, aptDate, visitType);
mDatabaseRef.child(id).setValue(appointment);

现在,在 onItemLongClick() 方法中:

patientListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Patient p = patientList.get(position);
        String patientId = p.getId();
        mDatabaseRef.child(patientId).removeValue(); // 移除特定对象
        return false;
    }
});

注:此方法仅适用于新添加的对象,因为只有这些对象包含 id。

英文:

To solve this, please change your class to:

public class Patient {
private String id;
private String name;
private String phone;
private String age;
private String describeCond;
private String gender;
private String date;
private String typeOfVisit;
private String docName;
public Patient(String id, String name, String phone, String age, String describeCond, String gender, String date, String typeOfVisit) {
this.id = id;
this.name = name;
this.phone = phone;
this.age = age;
this.describeCond = describeCond;
this.gender = gender;
this.date = date;
this.typeOfVisit = typeOfVisit;
}
public Patient(String id, String name, String phone, String age, String describeCond, String gender, String date, String typeOfVisit, String docName) {
this.id = id;
this.name = name;
this.phone = phone;
this.age = age;
this.describeCond = describeCond;
this.gender = gender;
this.date = date;
this.typeOfVisit = typeOfVisit;
this.docName = docName;
}
public Patient() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDescribeCond() {
return describeCond;
}
public void setDescribeCond(String describeCond) {
this.describeCond = describeCond;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTypeOfVisit() {
return typeOfVisit;
}
public void setTypeOfVisit(String typeOfVisit) {
this.typeOfVisit = typeOfVisit;
}
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
}

When you add the data to the database, use the following lines of code:

String id = mDatabaseRef.push().getKey();
Patient appointment = new Patient(id, name, phone, age, descCondition, gender, aptDate, visitType);
mDatabaseRef.child(id).setValue(appointment);

Now, in the onItemLongClick() method:

patientListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
Patient p = patientList.get(position);
String id = p.getId();
mDatabaseRef.child(id).removeValue(); //Remove that particular object
return false;
}
});

P.S. This will work only for the newly added objects because only those objects contain the id.

huangapple
  • 本文由 发表于 2020年4月5日 17:01:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61040215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定