如何更新Firebase数据?

时间:2017-04-18 18:39:52

标签: android database firebase firebase-realtime-database

我已将数据存储到我的firebase数据库,并且我想更新一个特定字段。我已经为此找到了docs,但我似乎无法使其适应我编码的方式。

在我的数据库中,有一个名为' clients'的孩子。在这个子项中,可以从我的应用程序创建客户端。我想编辑一个名为' image'的特定字段。

以下是我的数据库中客户端的示例,其中我想更改(突出显示)的字段: enter image description here

我有点卡住,提前谢谢! 我可以发布您需要的任何代码,感谢任何帮助。

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;
    }
}

1 个答案:

答案 0 :(得分:2)

要从Firebase数据库更新特定值,您需要关注树。在这种情况下,请使用以下代码:

String yourNewNam‌ = "Mike";
databaseClients.child(id).child("image").setValue(yourNewNam‌​e);

其中id必须是push()方法生成的唯一ID,yourNewNam‌​e是新名称。

请确认无效。