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/
Related
I pass my arraylist of strings from one activity to another using intents but I am able to only recover it by getCharSequenceArrayList. I am able to convert ArrayList of CharSequence to ArrayList String as below.
ArrayList<CharSequence> arrayList = getIntent().getExtras().getCharSequenceArrayList("employeesList");
ArrayList<String> arrayList1 = new ArrayList<>();
for (int i = 0;i<arrayList.size();++i)
{
arrayList1.add(arrayList.get(i).toString());
}
I was wondering if there was any concise way to convert the whole arraylist of charSequence to ArrayList Of String in one single step
Yes, the following code does exactly what you asked for
ArrayList<CharSequence> arrayList = getIntent().getExtras().getCharSequenceArrayList("employeesList");
//This line copies all elements of ArrayList to arrayList1
ArrayList arayList1 = new ArrayList<>(arrayList);
how can I save a shared preference to an array, I tried a couple of things but I just can't do it, I want to save a specific key into the array and then put them in a textview and it just crash.
please help guys.
public String[] fetchAllPreference(){
SharedPreferences sharedPref = getSharedPreferences("DictionaryInfo",
Context.MODE_PRIVATE);
//here i want the code of making it into array come
return values;
}
public void loadPicture()
{
gallery.removeAllViews();
String[] array =fetchAllPreference();
for(int i=0;i<array.length;i++)
{
TextView iv = new TextView(this);
LinearLayout.LayoutParams layoutParams=new
LinearLayout.LayoutParams(100,100);
layoutParams.setMargins(10,10,0,0);
iv.setLayoutParams(layoutParams);
iv.setText(array[i].toString());
gallery.addView(iv);
}
}
I know it isnt the exact answer, but it is possible to save each element of the array with the index being the keyword or index assossiated with some string being the keyword.
i think what u want is to save values in sharedpreference to an array, in that case
SharedPreferences prefs = getSharedPreferences("myFavs", 0);
Map<String, String> m = (Map<String, String>) prefs.getAll();
a map can be coverted to a collection
(https://developer.android.com/reference/java/util/Map.html)
and collection can be converted to an array
(https://developer.android.com/reference/java/util/Collection.html)
pls read this, it seems similar
Getting shared preferences and displaying them in a listview
I am using Gson library for pasring json,
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
PropertyModel[] response = gson.fromJson(reader, PropertyModel[].class);
i had set all data in Araylist like
Arraylist<PropertyModel[]> model=Arraylist<PropertyModel[]>();
model.add(response);
Now My problem is i am not able to get Arraylist PropertyModel class data
Please suggest me how can get Value from ArrayList
Thanks in Advance
try this code it will probably work...
String responseString = jsonArray.toString();
Log.e("jsonArray string --->", "" + responseString);
Type listType = new TypeToken<ArrayList<ModelOneVOneListItem>>() {
}.getType();
yourArrayList = new Gson().fromJson(responseString, listType);
First add values in Arraylist . Assume your modelclass name is CorpusHeadingModel:
ArrayList<CorpusHeadingModel> lemmaHeadingList = new ArrayList<CorpusHeadingModel>();
CorpusHeadingModel headingModel = new CorpusHeadingModel();
headingModel.setLemmaWord("Apple");
headingModel.setLemmaOccurance("3");
// now add the values to the list :
lemmaHeadingList.add(headingModel);
Now, Retrive one value with list position or any number corresponding the length of lemmaHeadingList :
CorpusHeadingModel model = lemmaHeadingList.get(position);// "position" or any number value according to your lemmaHeadingList.size().
String word = model.getLemmaWord();
String countWord= model.getLemmaOccurance();
Toast.makeText(mContext, "word= " +word+" Occurance= "+ countWord, Toast.LENGTH_SHORT).show();
Output:
word= Apple Occurance= 3
Hope, It helps, to retrieve any string from a list with modelClass.
Try like this
Add in Array list
ArrayList<String>() AddList= new ArrayList<String>();
Values = jb.getString("Your value");
AddList.add(Values);
Then Convert Array List to String Array
String[] BrandNameArray = new String[AddList.size()];
BrandNameArray = AddList.toArray(BrandNameArray);
use the Array
Your way is wrong assigning Array to Arraylist change last two lines:-
change these two line in the code
Arraylist<PropertyModel[]> model=Arraylist<PropertyModel[]>();
model.add(response);
to this:-
Arraylist<PropertyModel> model = new ArrayList<PropertyModel>(Arrays.asList(response));
For getting values you can use for loop
for(int i=0 ; i<model.size() ; i++){
PropertyModel object = model.get(i); //getting single object from Arraylist
}
as we know arraylist uses index to store values we must provide it an index but when its associated with a model class, it can be done in two ways.
let me give you an example.
1) you use your arraylist in the recycler adapter and get its object value using position,
holder.name.setText(myarraylist.get(position).getName()); // for recycler adapter in "onBindViewHolder"
name.setText(myarraylist.get(position).getName());//for base adapter in getview
2) if you want to check your object values in arraylist before passing it to the adapter you can do it this way,
for (contact d :emps ){
Log.e("name object of array",d.getName()+""); // getting the required object using the instance of model class
}
where contact is the model class, which is also the type of the arraylist emps. I hope this helps you. good luck
I would like to know how to remove duplicate values in Integer Array.
I think you may face This question in all languages.
But in android I don't know how to achieve this.Can anyone please help me to fix this issue.
Thanks in advance...
Be sure your array is Integer type not int
Integer[] array; // Your integer array...
Set<Integer> set = new HashSet<Integer>();
Collections.addAll(set, array);
val finalArrayList: ArrayList<String>
val arrayList = arrayListOf<String>()
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
finalArrayList = arrayList.toSet().toList() as ArrayList<String>
Answer
arrayList: [ABC, XYZ, ABZ, ABC, XYZ, ABZ]
finalArrayList: [ABC, XYZ, ABZ]
Try this Code
public static void removeDuplicateWithOrder(ArrayList arlList)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
arlList.clear();
arlList.addAll(newList);
}
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