Joe
Joe

Reputation: 253

How to update Firebase data?

I've stored data to my firebase database and I'd like to update one specific field. I've found the docs for this but I can't seem to adapt it to the way I've been coding.

Inside my database, there is a child called 'clients'. Within this child, clients can be created from my application. I'd like to edit a specific field called 'image'.

Here's an example of a client in my database with the field I'd like to change (highlighted): enter image description here

I'm a little stuck, thanks in advance! I can post any more code you need, any help is appreciated.

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

Upvotes: 0

Views: 138

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To update a particular value from your Firebase database you need to follow the tree. In this case, please use the following code:

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

In which id must be the unique id generated by the push() method and yourNewNam‌​e is the new name.

Please confirm it works.

Upvotes: 2

Related Questions