I want to make a Uri String like "http://almond.com/iOS/html/best.php?best_id=30241,15890"
The parameters of best_id (30241 and 15890) is retrieved like this from a custom url
String[] path = uri.getPath().split("/");
String sid = path[path.length - 1];
The problem is I want to keep appending a parameter with a comma after a click.
But I'm not sure how I can append a parameter to a url string and save it to the same sharedPreference.
sample
pref = getSharedPreferences("pref", MODE_APPEND);
SharedPreferences.Editor savestamp = pref.edit();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sid.length; i++) {
sb.append(sid[i]).append(",");
}
savestamp.putString("params2", sb.toString());
I'm trying to do it but since I'm a total noob,just googling wont help. I would be grateful if the pros here can help me out.
Commit after putting the values.
pref = getSharedPreferences("pref", MODE_APPEND);
SharedPreferences.Editor savestamp = pref.edit();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sid.length; i++) {
sb.append(sid[i]).append(",");
}
savestamp.putString("params2", sb.toString());
savestamp.commit();
please add savestamp.commit(); after savestamp.putString method then only it saved to SharedPreference
Related
i wish to, in one activity, put strings in the sharedpreferences, so then, in another activity, i get those strings, put them in a array and display them sequentially. I managed to do this, but i don`t have any idea how can i delete one specified string when asked. These strings will just be scattered on the shared preferences,and i dont know how to keep track of them. I can pass this unique int id to each element. I tried to use LinkedList, but i cannot pass this kind of structure as a shared preferences. I did not managed to make Gson work also. Please help.
Method that gets the string and put on shared preferences:
public void makefavorites(String[] a, String[] b, int id)
{
int idfinal = id%10;
idfinal = idfinal+1;
a[idfinal] = b[idfinal] +"\n" + "\n"+ a[idfinal];
SharedPreferences prefs = getSharedPreferences("Favorites", Activity.MODE_PRIVATE);
Editor edit = prefs.edit();
int temp = prefs.getInt("favorites_size", 0);
edit.putInt("favorites_size", temp+1);
edit.putString("array_" + prefs.getInt("favorites_size", 0), a[idfinal]);
edit.commit();
refreshfavorites();
}
Method that gets those strings, put on array and display it:
public void refreshfavorites()
{
SharedPreferences prefs = getSharedPreferences("Favorites", Activity.MODE_PRIVATE);
//GETS THE ARRAY SIZE
int size = prefs.getInt("favorites_size", 0);
String[] array = new String[size];
for(int i=0; i<size; i++){
array[i] = prefs.getString("array_" + i, null);
}
}
you have to use editor.remove method to delete specific value from arraylist..
public void removeArray(String[] list)()
{
SharedPreferences.Editor editor = mSharedPrefs.edit();
int size = list.length();
for (int i = 0; i < size; i++) {
editor.remove("favorites_size"+i);
}
editor.commit();
}
i hope its useful to you..
I am trying to take my 4 String arrays and convert them in to a single string separated by a ",". It seems to save the information easy enough when I debug; however, when I try and getString - it doesn't retrieve the data. I'm trying to determine why:
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
//Load Data
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
debtName = sharedPref.getString("debtNames", null).split(",");
//End Load
Bundle extras = getIntent().getExtras();
int flipper = 0;
for(int i=0;i<debtName.length;i++)
{
if(debtName[i].equals(null) && !extras.equals(null) && flipper!=1)
{
debtName[i] = extras.getString("newDebtName");
debtAmount[i] = extras.getString("newDebtAmount");
debtRate[i] = extras.getString("newDebtRate");
debtPayment[i] = extras.getString("newDebtPayment");
flipper = 1;
}
}
..............
StringBuilder value = new StringBuilder("");
SharedPreferences.Editor editor= sharedPref.edit();
for (String i : debtName) {
value.append(i + ",");
}
editor.putString("debtNames", value.toString());
for (String i : debtAmount) {
value.append(i + ",");
}
editor.putString("debtAmounts", value.toString());
for (String i : debtRate) {
value.append(i + ",");
}
editor.putString("debtRates", value.toString());
for (String i : debtPayment) {
value.append(i + ",");
}
editor.putString("debtPayments", value.toString());
editor.commit();
Correction, when run outside of debug, this gives a nullpointer exception on:
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
debtName = sharedPref.getString("debtNames", null).split(","); <---this line
Here is where the activity is called:
EditText editDebtName = (EditText) findViewById(R.id.dispDebtName);
debtName = editDebtName.getText().toString();
EditText editDebtAmount = (EditText) findViewById(R.id.dispDebtAmount);
String debtAmountStr = editDebtAmount.getText().toString();
EditText editDebtRate = (EditText) findViewById(R.id.dispDebtRate);
String debtRateStr = editDebtRate.getText().toString();
EditText editDebtPayment = (EditText) findViewById(R.id.dispDebtPayment);
String debtPaymentStr = editDebtPayment.getText().toString();
Intent i = new Intent(this, DebtList.class);
i.putExtra("newDebtName", debtName);
i.putExtra("newDebtAmount", debtAmountStr);
i.putExtra("newDebtRate", debtRateStr);
i.putExtra("newDebtPayment", debtPaymentStr);
startActivity(i);
The problem is with this line:
debtName = sharedPref.getString("debtNames", null).split(",");
It is possible that the key debtNames does not exist. Therefor null is returned.
This will happen when you run the app for the first time and have not saved anything to the shared preference yet.
Add a check for null and handle this case. Something like, if null is returned, then show default values, else show values that are read from sharedpreference.
I have surrendered to the apparent fact that there is no valid way to do this so I have hard coded 40 variables in to the app now. If anyone comes up with anything useful, please feel free to post it and I'll check this every so often.
I have got an array through Vector addittion like
[1!,2!,3!,4!]
I have to convert it into a string like
{1!2!3!4!}
..can you please tell me the name of few methods by which i can make it? Thanks all..
String getElement = null;
for(int j = 0;j<5;j++){
getElement = dynamicViewtagNames.elementAt(j);
}
I can get elements of this array like this..then I have to convert it into a string.
I'm not sure if I understand your question correctly, but if you just want to turn this:
[1!,2!,3!,4!]
into
{1!2!3!4!}
you can for example make use of the String.replace() or String.replaceAll() method.
String str = "[1!,2!,3!,4!]";
str = str.replace("[", "{");
str = str.replace("]", "}");
str = str.replace(",", "");
If [1!,2!,3!,4!] is a Vector containing the Strings you showed us above, you could do it like this using a StringBuffer:
// letz assume this vector has the following content: [1!,2!,3!,4!]
Vector<String> dynamicViewtagNames = new Vector<String>();
StringBuffer b = new StringBuffer();
b.append("{");
for(int i = 0; i < dynamicViewtagNames.size(); i++) {
b.append(dynamicViewtagNames.get(i))
}
b.append("}");
String mystring = b.toString();
Simple Solution
String names = names.replaceAll(",","");
names = names.replaceAll("[", "{");
names = names.replaceAll("]", "}");
Use this code,
StringBuilder str = new StringBuilder();
for (int i = 0; i<dynamicViewtagNames.length;i++){
str.append(dynamicViewtagNames[i])
}
str.toString();
or you can use:
Arrays.toString(dynamicViewtagNames);
Thanks
Hello I am new to Android development and I decided to work with the AndroidPlot library. To create a graph I need to enter in a number array like this
Number[] seriesOfNumbers = {4, 6, 3, 8, 2, 10};
What I need help with is creating that data in my app. My app runs a service once everyday and I want it to collect a certain number and add it to this array. Say for example something like this..
ArrayList<Integer> seriesOfNumbers = new ArrayList<Integer>();
seriesOfNumbers.add(5);
// Save the array
and then the next day retrieve this array and add another number to it and so on. Ive read that I should use SQLite but I am storing only one number each day. I cant create a new Array everyday because i need data from the previous days. What is the proper way to do this? Thanks
Edit:
This is as far as I got
public static void saveArray(Context ctx)
{
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences
.edit();
Number[] list = new Number[10];
StringBuilder str = new StringBuilder();
for (int i = 0; i < list.length; i++)
{
str.append(list[i]).append(",");
}
sharedPreferencesEditor.putString("string", str.toString());
sharedPreferencesEditor
.commit();
}
public void getArray(Context ctx)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String savedString = prefs.getString("string", "1");
StringTokenizer st = new StringTokenizer(savedString, ",");
for (int i = 0; i < 1; i++)
{
array[i] = Integer.parseInt(st.nextToken());
}
}
What I would like to do is be able to pass an integer through saveArray(Context ctx) and have it added to an array. Then it gets parsed into a string to be stored into shared preferences and then retrieved by getArray(Context ctx) where it gets recreated into an array if that makes any sense. Any help is very much appreciated Note: above code causes FC
Try something like this:
ArrayList<Integer> seriesOfNumbers = existsList() ? loadList() : new ArrayList<Integer>();
seriesOfNumbers.add(5);
saveList(seriesOfNumbers);
You just have to implement the ...List() - methods, maybe by using SqLite.
I have array values of up[]={0,0,0,0,0} and view="adult" thesetwo value i want to store and retrieve in from sharedpreference how to do that...
Assuming that you have a list of preferences for you app called MY_PREFS, I'd do this:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("arrayLength",up.size());
for(int i=0; i<up.size(); i++){
editor.putInt("up"+String.valueOf(i), up[i]);
}
editor.putString("view", "adult");
To retrieve them do:
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
int arraySize = settings.getInt("arrayLength");
int up[] = new int[arraySize];
for(int i=0; i<arraySize; i++){
up[i] = settings.getInt("up"+String.valueOf(i));
}
String view = settings.getString("view");
you can do by simple way, like this
SharedPreferences settings = getSharedPreferences("pref_name", 0);
SharedPreferences.Editor editor = settings.edit();
String values="";
for(int i=0; i<yourArray.length; i++){
values+=","+Integer.toString(yourArray[i]);
}
editer.putString("array",values);
editor.putString("view", "adult");
To get those values,
SharedPreferences settings = getSharedPreferences("pref_name", 0);
String[] strArray=settings.getString("array").split(",");
int[] yourArray=new int[strArray.length];
for(int i=0;i<strArray.length;i++){
yourArray[i]=Integer.toParseInt(strArray[i]);
}
Well as far as I have seen you can't directly store an array in shared preferences. You can however use a for loop to save an int with an increasing name and have it call it back the same way when you need it and store it into another array.
for(int i=0; i<numInYourArray; i++){
editor.putInt("up"+i, up[i]);
}
If you aren't sure on how to use Shared Preferences in general look here