How can i put a list to array - android

How can i put a list of strings, type string
in
String countryList[] = {a};
a is the variable that have the list
so, it will be
String countryList[] = {"heyyesGabon","heyyesGibraltar","heyyesGuinea"}; //and the others
this list comes from console.log in javascript
it always save the last item

From List to String array
String [] countryArray = countryList.toArray(new String[0]);
From String array to List
List<String> list = Arrays.asList(array);

You can use in-build function list.toArray(stringArray); it will return String[].
String countryList[] = new String[list.size()];
countryList = list.toArray(countryList);
Hope this will help you.

Related

How to convert a Arraylist of CharSequence to an ArrayList of String

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 to convert a string variable to a string array

I am making a request from my app to a server which is returning an array. I want to put that array into a listView but the the whole array is treated as a single listItem.
As the response is recieved in a string variable i want to convert it to a string array?
any help is appretiated.. Thanks
Just replace arr with your String Array variable name:
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Firstly string response data split by space[Available special character] or assign string array.
String yourdata = "A B C D E F G H";
String[] yourtDataArr = yourdata.split(" ");
ArrayList<String> list = new ArrayList<String>();
for(i =0;i<yourtDataArr.length;i++){
list.add(yourtDataArr[i]);
}
Then use your list on ListView. You can see following example.
http://www.vogella.com/tutorials/AndroidListView/article.html
https://www.tutorialspoint.com/android/android_list_view.htm
http://windrealm.org/tutorials/android/android-listview.php

Convert comma seperated String and replace existing Array List in android

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]);
}

Get Data from ArrayList Model class in android

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

String List array search for a string

Im having problems with searching through a list array trying to find certain string and then storing that string into a variable called name
private List<String> listview_array1 = new ArrayList<String>();
private List<String> listview_array2 = new ArrayList<String>();
private List<String> listview_array3 = new ArrayList<String>();
String name1 = null;
if(listview_array1.contains("The")) {
//assign value to name1
System.out.println(name1);
}
Documentation is your friend. In general the first line of any question of how do I use a class is look up it's docs.
http://developer.android.com/reference/java/util/ArrayList.html
As to your question you will want the indexof method. Details on the page above.

Categories

Resources