【发布时间】:2017-04-18 18:39:52
【问题描述】:
我已将数据存储到我的 firebase 数据库中,我想更新一个特定字段。我为此找到了docs,但我似乎无法使其适应我一直在编码的方式。
在我的数据库中,有一个名为“clients”的孩子。在这个孩子中,可以从我的应用程序创建客户端。我想编辑一个名为“图像”的特定字段。
这是我的数据库中的一个客户端示例,其中包含我想要更改的字段(突出显示):
我有点卡住了,提前谢谢! 我可以发布您需要的更多代码,不胜感激。
AddaClient.java:
public class AddProfileActivity extends AppCompatActivity {
EditText etName;
EditText etCareer;
CheckBox cbTech;
CheckBox cbMedi;
CheckBox nfRenewableEnergy;
CheckBox nfGoogle;
CheckBox cfNovartis;
CheckBox nfTesla;
CheckBox lrsFB;
CheckBox lrsAAPL;
CheckBox lrsYHOO;
CheckBox cbEURUSD;
CheckBox lrcuUSDRUB;
CheckBox lrcoSILVER;
CheckBox lrcoGOLD;
CheckBox lrcuGBPUSD;
CheckBox lriNSDQ;
CheckBox lriSP500;
Button bSave;
Spinner spinner5;
Button galleryspinner;
ImageButton bellbutton;
ImageView imageView10;
private ProgressDialog mProgress;
// String downloadAmberA = "http://res.cloudinary.com/liutavaras/image/upload/v1492506105/a_oufued.png";
// String downloadAmberB = "http://res.cloudinary.com/liutavaras/image/upload/v1492503937/b_eawxmg.png";
// String downloadAmberC = "http://res.cloudinary.com/liutavaras/image/upload/v1492506105/c_jouf2t.png";
// String downloadAmberD = "http://res.cloudinary.com/liutavaras/image/upload/v1492506105/d_isx7x3.png";
DatabaseReference databaseClients;
// DatabaseReference databasePhotos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
databaseClients = FirebaseDatabase.getInstance().getReference("clients");
// databasePhotos = FirebaseDatabase.getInstance().getReference("photos");
etName = (EditText) findViewById(R.id.etName);
etCareer = (EditText) findViewById(R.id.etCareer);
cbTech = (CheckBox) findViewById(R.id.cbTech);
cbMedi = (CheckBox) findViewById(R.id.cbMedi);
nfRenewableEnergy = (CheckBox) findViewById(R.id.nfRenewableEnergy);
nfGoogle = (CheckBox) findViewById(R.id.nfGoogle);
cfNovartis = (CheckBox) findViewById(R.id.cfNovartis);
nfTesla = (CheckBox) findViewById(R.id.nfTesla);
lrsFB = (CheckBox) findViewById(R.id.lrsFB);
lrsAAPL = (CheckBox) findViewById(R.id.lrsAAPL);
lrsYHOO = (CheckBox) findViewById(R.id.lrsYHOO);
cbEURUSD = (CheckBox) findViewById(R.id.cbEURUSD);
lrcuUSDRUB = (CheckBox) findViewById(R.id.lrcuUSDRUB);
lrcoSILVER = (CheckBox) findViewById(R.id.lrcoSILVER);
lrcoGOLD = (CheckBox) findViewById(R.id.lrcoGOLD);
lrcuGBPUSD = (CheckBox) findViewById(R.id.lrcuGBPUSD);
lriNSDQ = (CheckBox) findViewById(R.id.lriNSDQ);
lriSP500 = (CheckBox) findViewById(R.id.lriSP500);
bSave = (Button) findViewById(R.id.bSave);
ImageButton bSettingsBlack = (ImageButton) findViewById(R.id.ibSettingsBlack);
ImageButton bEconCalBlack = (ImageButton) findViewById(R.id.ibEconCalBlack);
ImageButton bLiveRates = (ImageButton) findViewById(R.id.ibLiveRates);
ImageButton bNewsBlack = (ImageButton) findViewById(R.id.ibNewsBlack);
bSettingsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, SettingsActivity.class));
}
});
bEconCalBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, WebViewActivity.class));
}
});
bLiveRates.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, sortingrates.class));
}
});
bNewsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(AddProfileActivity.this, HomePageNews.class));
}
});
bSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bSave();
}
});
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView10;
public DownloadImageTask(ImageView imageView10) {
this.imageView10 = imageView10;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
imageView10.setImageBitmap(result);
}
}
private void bSave(){
String name = etName.getText().toString().trim();
String career = etCareer.getText().toString().trim();
Boolean techCB = cbTech.isChecked();
Boolean mediCB = cbMedi.isChecked();
Boolean renewableEnergyNF = nfRenewableEnergy.isChecked();
Boolean googleNF = nfGoogle.isChecked();
Boolean novartisNF = cfNovartis.isChecked();
Boolean teslaNF = nfTesla.isChecked();
Boolean fbLRS = lrsFB.isChecked();
Boolean applLRS = lrsAAPL.isChecked();
Boolean yhooLRS = lrsYHOO.isChecked();
Boolean eurusdCB = cbEURUSD.isChecked();
Boolean usdrubCU = lrcuUSDRUB.isChecked();
Boolean silverCO = lrcoSILVER.isChecked();
Boolean goldCO = lrcoGOLD.isChecked();
Boolean gbpusdCU = lrcuGBPUSD.isChecked();
Boolean nsdqI = lriNSDQ.isChecked();
Boolean sp500I = lriSP500.isChecked();
String image = "joe";
if(!TextUtils.isEmpty(name)){
String id = databaseClients.push().getKey();
Clients clients = new Clients (id, name, career, techCB, mediCB, renewableEnergyNF, googleNF,
novartisNF, teslaNF, fbLRS, applLRS, yhooLRS, eurusdCB, usdrubCU, silverCO,
goldCO, gbpusdCU, nsdqI, sp500I, image );
databaseClients.child(id).setValue(clients);
Toast.makeText(this, "Client added", Toast.LENGTH_LONG).show();
startActivity(new Intent(AddProfileActivity.this, SettingPriority.class));
}else{
Toast.makeText(this, "Please enter your name", Toast.LENGTH_LONG).show();
}
}
}
Clients.java:
public class Clients {
String clientName;
String clientCareer;
Boolean techCB;
Boolean mediCB;
Boolean renewableEnergyNF;
Boolean googleNF;
Boolean novartisNF;
Boolean teslaNF;
Boolean fbLRS;
Boolean applLRS;
Boolean yhooLRS;
Boolean eurusdCB;
Boolean usdrubCU;
Boolean silverCO;
Boolean goldCO;
Boolean gbpusdCU;
Boolean nsdqI;
Boolean sp500I;
String image;
// public Clients(String id, String name, String career, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF, Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB, Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I, String valueOfSelectedPos){ }
// public Clients(String id, String name, String career, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF, Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB, Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I) {
//
// }
public Clients(String id, String clientName, String clientCareer, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF,
Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB,
Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I, String image ) {
this.clientName = clientName;
this.clientCareer = clientCareer;
this.techCB = techCB;
this.mediCB = mediCB;
this.renewableEnergyNF = renewableEnergyNF;
this.googleNF = googleNF;
this.novartisNF = novartisNF;
this.teslaNF = teslaNF;
this.fbLRS = fbLRS;
this.applLRS = applLRS;
this.yhooLRS = yhooLRS;
this.eurusdCB = eurusdCB;
this.usdrubCU = usdrubCU;
this.silverCO = silverCO;
this.goldCO = goldCO;
this.gbpusdCU = gbpusdCU;
this.nsdqI = nsdqI;
this.sp500I = sp500I;
this.image = image;
}
public String getImage() {
return image;
}
public String getClientName() {
return clientName;
}
public String getClientCareer() {
return clientCareer;
}
public Boolean getTechCB() {
return techCB;
}
public Boolean getMediCB() {
return mediCB;
}
public Boolean getRenewableEnergyNF() {
return renewableEnergyNF;
}
public Boolean getGoogleNF() {
return googleNF;
}
public Boolean getNovartisNF() {
return novartisNF;
}
public Boolean getTeslaNF() {
return teslaNF;
}
public Boolean getFbLRS() {
return fbLRS;
}
public Boolean getApplLRS() {
return applLRS;
}
public Boolean getYhooLRS() {
return yhooLRS;
}
public Boolean getEurusdCB() {
return eurusdCB;
}
public Boolean getUsdrubCU() {
return usdrubCU;
}
public Boolean getSilverCO() {
return silverCO;
}
public Boolean getGoldCO() {
return goldCO;
}
public Boolean getGbpusdCU() {
return gbpusdCU;
}
public Boolean getNsdqI() {
return nsdqI;
}
public Boolean getSp500I() {
return sp500I;
}
}
【问题讨论】:
-
要更新 Firebase 数据库中的特定值,请使用以下代码:databaseClients.child(id).child("image").setValue(yourNewName);
-
太棒了!谢谢。这几乎是完美的。但是,它会删除客户端内部的所有内容(姓名、职业等)。我该如何专门更改图像?再次感谢亚历克斯! @AlexM。
-
如果你像@AlexM一样正确使用了setValue方法。建议,不应删除其余数据,确保您在 databaseClients.child(id) 中传递正确的 id...
-
嗯。我试图在不同的活动中运行它,我正在使用'String id = databaseClients.push().getKey();'。如果有帮助,我可以发布我的其他活动的完整代码。我有点新手哈哈!干杯@wnieves19
标签: android database firebase firebase-realtime-database