0

I want to take the values one by one from my String to print it

  StringBuilder t = new StringBuilder();
    for(int l=0; l<=itemList.length(); l++){
        Log.d("SPA", "PRINT LOOP " +t);
        Log.d("SPA", "PRINT LOOPP " +l);

    }

This is what i have tried and it is not working.

I am using Shared Preference to take multiple values from previous class from JSON

To retrieve the values from SHARED PREFERNECE

SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME1", Context.MODE_PRIVATE);

    ServicesFromDeatailsPage=(mySharedPreferences.getString("yourKey", null));

To Make it into String :

 String itemList = Stream.of(ServicesFromDeatailsPage )
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining("\n"));

output is:

list is {"0":"Hair","1":"Makeup","2":"Nail"}

Now what i want is to take values one by one from each index and save it in database with just one button click.

Can i do it with for loop and if yes, how? or is there any other way to do this in android studioThis is what i want . This is what iam geting

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
  • What kind of database is this? – MarkWalczak Jul 25 '19 at 08:48
  • Firebase Database // final DatabaseReference ReserveSeat = FirebaseDatabase.getInstance().getReference("Reservation").child(NameOfSpa).child(DATE).child("Services").child(Userid).child(ServicesFromDeatailsPage); – Ayesha Israr Jul 25 '19 at 08:54
  • Hi, You want to make one Item per indexed object in your layout, insn't it ? I think you'll need to make a layout for your item object and then infalte it in your parent one. https://stackoverflow.com/questions/3477422/what-does-layoutinflater-in-android-do – Gweltaz Niquel Jul 25 '19 at 08:56
  • No i already have a layout i just want to take the items from my string and add it to database – Ayesha Israr Jul 25 '19 at 09:11

3 Answers3

0

You don't have a String, but JSONObject. So try this:

JSONObject jsonObj = new JSONObject(itemList);

for (int i=0; i < jsonObj.length(); i++)
{
    try {
        String itemInArray = jsonObj.getString(i);
          myRef.child(itemInArray).value(YOUR_VALUE);
    } catch (JSONException e) {
        // Oops
    }
}
MarkWalczak
  • 1,532
  • 3
  • 16
  • 24
0
**Try this its worked for me**

    JSONObject itemList = jObj.getJSONObject("itemList");

    Iterator<String> iterator = itemList.keys();
    while(iterator.hasNext())
    {
        String key = iterator.next();
        Log.e("######", "key " + key + "Value " + itemList.optString(key));
    }
satyan_android
  • 364
  • 4
  • 15
0
--using shared preferences we can save and retrieve value, like this --

define global variable--
SharedPreferences sharedPreferences;

in ApiClient where you put all your api-

add this-

public static final String IS_User_LOGIN = "userlogin";

in onCreateview() of activtiy- 

//shared preferences
        if (getSharedPreferences("Login", 0).getBoolean(IS_User_LOGIN, false))

        {
            intent = new Intent(getApplicationContext(), HomeActivity.class);
            startActivity(intent);

        }
to save data when user login-

sharedPreferences = getSharedPreferences(SHARED_PREF, MODE_PRIVATE);

like i saved data in  api response we can save data --

 //startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    intent = new Intent(LoginActivity.this, HomeActivity.class);

                    //shared preferences
                    SharedPreferences mPrefsLogin = getApplicationContext().getSharedPreferences("Login", 0);
                    SharedPreferences.Editor prefsEditorLogin = mPrefsLogin.edit();
                    prefsEditorLogin.putBoolean(IS_User_LOGIN, true);
                    prefsEditorLogin.apply();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    Gson gson = new Gson();
                    String Json = gson.toJson(response.body().getUser());
                    editor.putString("data", Json);
                    editor.apply();
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();

after that in next activity or class to retrive data like i used homeactivty-

define global variable-

SharedPreferences sharedPreferences;

define static variable so you can access in other classes too-

public static String SPUSER_ID, DEVICE_TOKEN;


in onCreate() -

get data -

 //shared preferences

        sharedPreferences = getApplicationContext().getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
        String json = sharedPreferences.getString("data","");
        Gson gson = new Gson();
        User obj = gson.fromJson(json,User.class);

        SPUSER_ID = sharedPreferences.getString("userid","");
        DEVICE_TOKEN = sharedPreferences.getString("devicetoken","");


that's it you can get value 
s.j
  • 621
  • 6
  • 16