Converting array to string array and setting data to Spinner in android..? - android

As I am new to android development i struck up with a problem where i am unable to set data to spinner adapter..
Here i am getting data from database is like
String times = [09:30,10:30,12:15,04:45,10:50]
I am getting array in this pattern. When i am trying to set this array to spinner adapter it's getting error...
dateadp=new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item,times);
datespn.setAdapter(dateadp);
so how to convert the following array to string array and how i can append that data to spinner. Can anyone help me with this....

If
["09:30","10:30","12:15","04:45","10:50"] represents String abc.
Then you can take following approach.
String processingString = abc.substring(abc.indexOf("[") + 1,
abc.indexOf("]"));
String[] arr = processingString.split(",");
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this,
android.R.layout.simple_list_item_1, arr);

I think you struck with converting arraylist to String array. if you are getting data into the arraylist then the code is here.
ArrayAdapter<String> dateadp;
timesArray= times.toArray(new String[times.size()]);
dateadp=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,timesarray);
datespn.setAdapter(dateadp);

Related

Removing Key String from JSON Array when converting to List

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

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.

how to use variable size array in android?

I'm using string array which I'm passing to ArrayAdapter for spinner item.
my array size(usually less than 10) and values are variable (I'm taking it from asset file)
if I'm using this :
String[] arr = new String[10];
protected void onCreate(Bundle savedInstanceState) {
breader = new BufferedReader(new InputStreamReader(getAssets().open(path)));
int length = Integer.parseInt(breader.readLine());
for(int i=0; i<length; i++){
arr[i]=breader.readLine();
initialize();
....
}
initialize(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> array = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, arr);
spinner1.setAdapter(array);
}
it shows NullPointerException at spinner1.setAdapter(array); line.
can I reassign array length. I think its not possible.
because of you add null when no data in last portion of array.
I mean there are 7 data in assest file and you move for loop 10 times so after 7 data Add there are null represent. so breader.readLine(); read null and add it to your string array and when you parse StringArray to Array Adapter there are null get on 8th position of array and nullpointer Exception fire.
So just check breader.readLine() != Null then add it to String of Array otherwise add some Temp text.
Or
You Also Use ArrayList instead of String[] Array.
ArrayList is dynamically Arraylist you can add data to list useing array.add(yourdata).
Thats it...

How to populate a Spinner from String array

How can i populate a Spinner from String array , I know i can do that from array.xml like this code :
ArrayAdapter<CharSequence> gameKindArray = ArrayAdapter.createFromResource(view.getContext(),R.array.game_kind, android.R.layout.simple_spinner_item);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
but when i have
String[] test=new String[]{"test1","test2"};
how i can change String[] to ArrayAdapter ?!
Use ArrayAdapter this way
your string array
String[] test=new String[]{"test1","test2"};
your ArrayAdapter
ArrayAdapter<String> gameKindArray= new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
Try the below
ArrayAdapter<String> gameKindArray = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, test);
gameKind.setAdapter(adapter);
For more info check the docs.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/reference/android/widget/Spinner.html
you dont need to convert in any form just use your string array like below:
ArrayAdapter<String> gameKindArray= new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
you can get easily into spinner.

Force closing if i add a string to string array

I have the code as below
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ListView lv = new ListView(this);
myList[4] = "hi";
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
The app is force closing,in logs im getting "java.lang.UnsupportedOperationException" if i remove myList[4] = "hi"; code I'm getting the listview as in myList array. But my problem is i have to add string dynamically to this array and have to display.
Don't use array. Array has fixed length and you cannot add new items to it. Use list instead:
List<String> myList = new ArrayList<String>();
myList.add("Hello");
myList.add("World");
myList.add("Foor");
myList.add("Bar");
ListView lv = new ListView(this);
myList.add("hi");
You cannot simply add another element to the array. When you declare your array as
String[] myList = new String[] {"Hello","World","Foo","Bar"};
Your array is created with four elements in it. When you are trying to set myList[4], you're essentially trying to set a fifth element - and are obviously getting ArrayIndexOutOfBoundsException.
If you need to dynamically add elements, you are better off using ArrayList instead of an array.
Since your array contains 4 items, myList[4] is actually out of bounds. The maximum element index will be myList[3]. Sure this is the issue.
you are trying to add element at 5th position. Arrays don't grow dynamically. why dont' you try like this,
String[] myList = new String[] {"Hello","World","Foo","Bar"};
ArrayList<String> mList=new ArrayList<String>();
Collections.addAll(mList,myList);
ListView lv = new ListView(this);
mList.add("Hi");
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mList));
setContentView(lv);

Categories

Resources