Is there anyway I can save a List variable to the Androids phone internal or external memory? I know I can save primitive data, yet not sure about this one.
Thanks in advance.
Yes exactly you can only save primitives so you could something like this:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
StringBuilder csvList = new StringBuilder();
for(String s : list){
csvList.append(s);
csvList.append(",");
}
sharedPreferencesEditor.put("myList", csvList.toString());
Then to create your list again you would:
String csvList = sharedPreferences.getString("myList");
String[] items = csvList.split(",");
List<String> list = new ArrayList<String>();
for(int i=0; i < items.length; i++){
list.add(items[i]);
}
You could encapsulate this "serializing" into your own class wrapping a list to keep it nice and tidy. Also you could look at converting your list to JSON.
I use ObjectOutputStream to save lists to storage.
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(your_file));
oos.writeObject(your_list);
oos.flush();
oos.close();
That should do it. It works for me
If you want to store/retrieve/delete some string value in your program, then List is better than Array. Here below I will show you that how to use it.
// Declaring List Variable
List<String> Mylist = new ArrayList<String>();
// Adding item to List
Mylist.add(list.size(), "MyStringValue"); //this will add string at the next index
// Removing element form list
Mylist.remove(list.size()-1); //this will remove the top most element from List
Related
i have String Array like this:
String[] q1={"AAA-BBB","AAA-CCC","AAA-DDD"}
and i want result like this
temp={"BBB","CCC","DDD"}
i tried below code but the result is wrong
for(int i=0;i<q1.length;i++){
ArrayList<String> temp=new ArrayList<>(Arrays.asList(q1[i].split("AAA-")));
}
Try like this:
ArrayList<String> temp=new ArrayList<>();
for(int i=0;i<q1.length;i++){
String[] array = q1[i].split("-");
temp.add(array[1]);
}
You could use substring:
ArrayList<String> temp = new ArrayList<>();
for(int i=0; i<q1.length; i++){
temp.add(q[i].substring(q[i].indexOf('-') + 1, q[i].length()))
}
you find error Because you use split
Splits this string around matches of the given regular expression.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
q1[i].split("AAA-")
in this line you got 2 result splited 0 = "" AND 1 = "BBB"
so you need to pick the sec result
you have multi Solution
like https://stackoverflow.com/a/50234408/6998825 said
String[] array = q1[i].split("-");
temp.add(array[1]);
//change this q1[i].split("AAA-") to
q1[0].substring(4)
if your AAA- is not going to change
Have you tried creating the ArrayList outside of the loop? As previously you were creating a new ArrayList for every element in your string array
ArrayList<String> temp = new ArrayList<>();
for(int i=0;i<q1.length;i++){
temp.add(q1[i].substring(4);
}
Assuming that "AAA-" is not going to change.
Hey stackoverflow community,
i want to send an Arraylist of Textviews through shared Preferences to another Activity, so i tried to convert my list into a HashSet.
This is not accepted:
public List<TextView> FavDishes = new ArrayList<TextView>();
.
.
.
FavDishes = new ArrayList<>();
FavDishes.add(eingabe);
**Set<String> taskSet = new HashSet<String>(FavDishes);**
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putStringSet("Data", taskSet)
.apply();
It tells me: "cannot resolve constructor HashSet on android widget Textview"
How would you solve this Problem?
Thanks for your time.
Iterate over the FavDishes array to get the TextView values as string and store them to an array, then pass that array to your taskset instead of FavDishes.
Code will be something like:
List<TextView> FavDishes = new ArrayList<TextView>();
FavDishes.add(fav1TV);
FavDishes.add(fav2TV);
FavDishes.add(fav3TV);
ArrayList<String> FavDishesString = new ArrayList<String>();
for (TextView FavDish : FavDishes ){
FavDishesString.add(FavDish.getText().toString());
}
Set<String> taskSet = new HashSet<String>(FavDishesString);
Helpful link to ways to iterate arrayList:
https://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
I am rather new to JSON at the moment, but I need to convert a JSON response that contains the same key, but different values to an ArrayList to use it with my spinner.
I tried it like here: Converting JSONarray to ArrayList
But i get the whole json string, but just need the value part.
I can't figure out how to do this and found no answer that worked for me :/
What I want would be a List like:
City1
City2
City3
But i have in my spinner:
{"city":"name1"}
{"city":"name2"}
{"city":"name3"}
Code I have is:
JSONArray obj = new JSONArray(response);
Spinner availableCitySpin;
availableCitySpin = (Spinner) findViewById(R.id.avCitySp);
List<String> cityValues = new ArrayList<String>();
if (jarr != null) {
for (int i=0;i< jarr.length();i++){
cityValues.add(jarr.getString(i).toString());
}
}
ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cityValues);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
availableCitySpin.setAdapter(cityAdapter);
availableCitySpin.setSelection(0);
Change your code to something like this:
...
for (int i=0;i< jarr.length();i++){
JSONObject cityObject = jarr.getJSONObject(i);
cityValues.add(cityObject.getString("city"));
}
...
Try this:
First use split
For example: String[] result = splits[0].split(":");
you will get two item in array result. result[0]= {"city" and result[1] = "name1"}
If you want to get the key use result[0]
remove sign from result[0] using replace. Example: data = result[0].replace("\"","").replace("{","");
use it in loop, should work
We have an Array List consisting of many strings.
public final static ArrayList<String> MyListArrayList= new ArrayList<String>(){{}};
And we converted the array list into a comma separated string using below code
gson = new Gson();
listOfTestObject = new TypeToken<ArrayList<String>>(){}.getType();
MyListString = gson.toJson(Images.MyListArrayList, listOfTestObject);
Now we have a comma separated string like
"Item1,Item2,Item3,Item3.....Itemn"
and we are saving this string using the preference whenever the array list gets updated. So now we need to load this string at starting and then convert the string back into the array List.For that we are trying below code
List<String> Images.MyListArrayList= new ArrayList<String>(Arrays.asList(s.split(",")));
But its not possible to do it.But we can create a new List.. But we want to update the already existing Array List instead of creating new one. How to do it?
String value="Item1,Item2,Item3,Item3.....Itemn";
String[] arrayList = value.split(",");
ArrayList list = new ArrayList<String>();
for (int i = 0; i < arrayList.length; i++) {
list .add(arrayList[i]);
}
I want to add AutoCompleteTextView in my application. I have one txt file in that there are more than 2000 records are present. I want to use it for AutoCompleteTextView. Normally for small data we use array as:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,dataArray);
AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv.setThreshold(1);
actv.setAdapter(adapter);
But now how to use txt file for AutoCompleteTextView. Any suggestion will be appreciated.
How are you separating elements in your text file? Assuming that you have a new elements on each line you can use this to convert the file to an array, the use the array adapter as you have mentioned above.
String[] arr= null;
List<String> items= new ArrayList<String>();
try
{
FileInputStream fstream = new FileInputStream("text1.txt");
DataInputStream data_input = new DataInputStream(fstream);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String str_line;
while ((str_line = buffer.readLine()) != null)
{
str_line = str_line.trim();
if ((str_line.length()!=0))
{
items.add(str_line);
}
}
arr = (String[])items.toArray(new String[items.size()]);
}
create one string resource file like "string_autocompletearray" and define your array.
**res/values/string_autocompletearray.xml**
string-array name="autocomplete_array">
<item>AutoCompleteText 1 </item>
<item>AutoCompleteText 2 </item>
-------------------------------
-------------------------------
<item>AutoCompleteText N </item>
</string-array>
**Now find this array and set to adapter**
ArrayList<String> list = new ArrayList<String> (Arrays.asList(getResources().getStringArray(R.array.autocomplete_array)));