Why it doesn't work (string array)? - android

Hi I wanted to add new string to string array by this method but it doesn't work, because when I start new activity, then favaorites array isn't updated. Why ?
Resources res = getResources();
String[] favorites = res.getStringArray(R.array.favorites);
String[] planets = res.getStringArray(R.array.planets_array);
String[] temp = new String[favorites.length+1];
System.arraycopy(favorites,0,temp,0,favorites.length);
temp[favorites.length] = planets[mCounter];
favorites = temp;

In your case what you can do is use SharedPreferences to store the string into it. No array assigns requires and it is a much cleaner way to do it. Some links to get you started:
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://developer.android.com/guide/topics/data/data-storage.html

To solve your problem, you should be create an SQLite Database that houses all these properties. Then you should retrieve them from the Cursor (after you query your database) and use the results as necessary
Also another note is that you CANNOT add to the already defined R.array.* because its a precompiled resource.

Related

Add a lot of images dynamically to SQLite Database Android

I hope you are good.
I'm developping an application that has to retrieve images from sqlite database, for each row I have one image (using obviously blob as a type), and I was wondering if I could not only add one image but a lot, without knowing how much images the user wants to add, for example: User1 wants to add 3 images to database. User2 5 images and so on. How can I store them? is there a kind of Blob[] or something? And if possible how can I choose more than one image from galery?
I'm not used to make posts in english so I hope I explained well ^^'.
Thank you.
Save the images in a list as base64 strings like:
String img1 = Base64.encode(value_of_image, Base64.DEFAULT);
.
.
.
String img25 = Base64.encode(value_of_image, Base64.DEFAULT);
List<String> imagesListActivity = new ArrayList();
imagesList.add(img1);
.
.
.
imagesList.add(img25);
and in your save method in helper class, you can do sth like:
public void saveImageList(List<String> images){
ContentValues cv = new ContentValues();
for(int i = 0; i < images.length; i++){
cv.put(COL_NAME, images[i);
}
}
and in activity class, try this by passing the actual list like below:
DBHelper dbHelper = new DBHelper();
dbHelper.saveImageList(imagesList);

How to use getIdentifier() on a string array for strings from resources

I have a string in a database that is in the layout of the following:
String fromdatabase = "one,two,three,four";
Basically I want to get this to the following format for further processing and use:
String[] array = new String[]{R.string.one,R.string.two,R.string.three,R.string.four};
So far I have the following code that converts the string to a string array, but do not know how to go from there. I was thinking a for-loop with some form of
getResources().getIdentifier(???,"string","package"));
then putting it into a new string array at the end, but I do not know where to start with this.
String[] split_fromdatabase = fromdatabase.split("\\s*,\\s*");
Thanks
After an hour of trial an error, this code seems to work.
It may not be the best written, but it works:
String[] split_fromdatabase = dropped_by.split(",");
a = new ArrayList<String>();
for(int i1=0; i1 <Adropped_by.length; i1++){
System.out.print(Adropped_by[i1]);
int res = getResources().getIdentifier(split_fromdatabase [i1],"string",getPackageName());
a.add(getString(res));
}
db = new Object[a.size()];
db = a.toArray(db);
R.string.one means you are pointing to a string in 'res' folder.
You can not create Strings(or anything) in res folder at runtime.

How to retrieve data from ArrayList<ModelObject> not from ArrayList<String>?

Hi I'm still new to java data management.
I have a model object class named Computer which has 3 fields: processor, ram, hddSize.
I created a ArrayList
ArrayList<Computer> myCompList = new ArrayList<Computer>();
Computer comp1 = new Computer();
comp1.setProcessor("1.5 GHZ");
comp1.setRam("512 MB");
comp1.setHddSize("100 GB");
Computer comp2 = new Computer();
comp2.setProcessor("2.5 GHZ");
comp2.setRam("512 MB");
comp2.setHddSize("50 GB");
myCompList.add(comp1);
myCompList.add(comp2);
Now How can I retrieve data at index1 of the ArrayList above?
PS: I know how to do it if its a ArrayList< String> by convert it to String[] and then String[index].
Look at the Javadocs for ArrayList
This is where you should check for simple questions like this. The answer can be found in the "Method Summary" section.
Assuming that you have created getters and setters in your Computer class:
String processor = myCompList.get(1).getProcessor();
String ram = myCompList.get(1).getRam();
String hddSize = myCompList.get(1).getHddSize();
Can't you just go myCompList.get(0); ?
An arraylist of objects is essentially the same as an arraylist of strings. The .get() method returns the specific object at the given index. Here is the documentation for ArrayList.
myCompList.get(index) will return you data on the given index, make sure index number wouldn't be greater than array size, it will give you index out of bounds exception.

Calling a Resource by a string?

Here's the setup. I have a spinner, and each item in the spinner is associated with their own StringArray. I want to streamline the process of loading the StringArray when an item is selected in the spinner without using a bunch of if statements for each item.
The StringArray has the same name as the spinner item's text
Drawn out it would look like this:
String cat = parent.getItemAtPosition(pos).toString(); //Selected Spinner item (Category)
...
String catStringArray = "R.array." + cat;
listdata = getResources().getStringArray(catArray); //Get the StringArray
is there a way to do this correctly?
--Edit--
#EboMike
Your answer sent me on a hunt and ran into this which I'm now using:
Class res = R.array.class;
Field field = res.getField(selectedCategory);
int saId = field.getInt(null);
String[] myList = getResources().getStringArray(saId);
That's not a great approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs or something similar.
If you really insist on using a string-based approach, use Resources.getIdentifier(). It's technically not a big deal if you only do it once.

android parcelable string array

I have
ArrayList<String> ids = ArrayList<String>();
What would be the cleanest way to make it Parceleable? Apparently String itself is not parcelable, so Parcel.writeList(ids) is not working.
I was thinking to either parcelize ArrayList<Uri> or put array contents into a Bundle.
Convert your list to String[] and use Parcel.writeStringArray. Example here
This isnt parcelizing, but to put it in a bundle:
ArrayList<String> ids = new ArrayList<String>();
savedInstanceState.putStringArrayList("key",ids);

Categories

Resources