Cannot cast from String[] to ArrayList<String> - android

I have a resource string array that I want to put into an ArrayList, however the datatype that comes back is a String array. The string[] cannot be directly cast to ArrayList w/o receiving the following error:
Cannot cast from String[] to ArrayList
How to I convert the String[] datatype to ArrayList?
Edit
I have an adapter whose constructor takes an ArrayList and a resource string-array that I want to populate it.

If you can make do with a List<String> (and not specifically an ArrayList<String>), you can use:
List<String> list = Arrays.asList(stringArray);
Otherwise, you can do this:
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
However, the latter is less efficient (in both time and object creation count) than the other suggested solutions. Its only benefit is that it keeps the code down to one line and is easy to comprehend at a glance.

public ArrayList<String> arrayToArrayList(String[] array){
ArrayList<String> arrayList = new ArrayList<String>(array.length);
for(String string : array){
arrayList.add(string);
}
return arrayList;
}

how about this
import java.util.Collections;
String[] myStringArray = new String[] {"foo", "bar", "baz"};
List myList = new ArrayList(myStringArray.length);
Collections.addAll(myList, myStringArray);

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

Split strings on string array

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.

Array list wont display in my listview Android

Please someone help me how to show all list array in listview, i have "eventname" which values are: "Party", "Study", "Exam".
and inside my for loop i have this codes and it only outputs "Exam"..
lv = (ListView) findViewById(R.id.textView);
List<String> your_array_list = new ArrayList<String>();
your_array_list.add(eventname);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(CalendarActivity.this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
You are not adding data in your arraylist correctly..you are adding only one data in arraylist.
String[] array ={"EXAM","Study"};
for(String str: array){
your_array_list.add(str);
}
You code works properly, you are adding just one item:
your_array_list.add(eventname);
If you want to add the items "Party", "Study", "Exam", you should add them one by one:
your_array_list.add("Party");
your_array_list.add("Study");
your_array_list.add("Exam");
Or much better, declare them in a constant array and add them in a loop:
String[] ITEMS = new String[]{"Party", "Study", "Exam"};
...
for(String item : ITEMS) {
your_array_list.add(item);
}
Also, consider using an enum instead of a constant array. But it depends on what you are specifically programming, of course.

Load SimpleCursorAdapter from an Array

Is it possible to load a SimpleCursorAdapter from an Array of data i have made?
This is how a load my Array:
String[][] arrayStixoi = new String[countCategory][countCategory];
int i = 0;
while(ccget.moveToNext()){
String stixoi = ccget.getString(ccget.getColumnIndex("stixoi"));
String syggraf=ccget.getString(ccget.getColumnIndex("syggrafeas"));
String notes=ccget.getString(ccget.getColumnIndex("syggrafeas"));
String news=ccget.getString(ccget.getColumnIndex("syggrafeas"));
arrayStixoi[i][0] = Integer.toString(i);
arrayStixoi[i][1] = stixoi;
arrayStixoi[i][2] = syggraf;
arrayStixoi[i][3] = notes;
arrayStixoi[i][4] = news;
i++;
String _id=arrayStixoi[i][0]+1;
}
Now i want that Array: arrayStixoi to load a SimpleCursorAdapter and then add this adapter to a ListView.
Is this possible? and how?
Thank you in advance for your time.
In your code snippet you have
ccget.moveToNext()
I assume ccget is an instance of Cursor. With that, you can just instantiate a SimpleCursorAdapter using its default constructor:
new SimpleCursorAdapter(
this, R.layout.your_layout,
ccget,
from,
to,
flags)
Please refer to the official documentation.

adding objects to a ArrayList<String>

I believe you can add objects use list.add(); However, is there an alternative way of making an ArrayList of strings? Something like ArrayList("heyey","hgfhgfh","fhfghgf") ?
List<String> listString = Arrays.asList(new String[] {"heyey","hgfhgfh","fhfghgf"});
With listString being a fixed size list (see Arrays.asList).
If you need a variable size list:
List<String> listString = new ArrayList<String<(Arrays.asList(new String[] {"heyey","hgfhgfh","fhfghgf"}));
List<String> strings = Arrays.asList("ohai", "I", "have", "varargs");
Or if you're kickin' it old school:
List strings = new ArrayList() {{
add("ohai");
add("Java 1.4");
add("lives");
}};

Categories

Resources