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);
Related
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.
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...
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);
How can I convert from String[] to String only? So I can assign orderan to items and i can put it into my listview.
Here's my code.
MyClass class= new MyClass();
String orderan =class.getName();
String[] items = orderan; **the problem here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
It appears that you simply want this:
String orderan = class.getName();
String[] items = {orderan};
Or even:
String[] items = {class.getName()};
ArrayAdapter wants an array, but you're trying to assign the string orderan into the array items. Instead, make a new array, and add orderran into it. Something like:
String[] items = new String[1];
items[0] = orderan;
You have to create an Array and add the string to it.
String[] items = new String[1];
items[0] = orderan;
you can do it in one line
String[] items = {orderan};
I just trying to set value to spinner manually by using ArrayAdapter,
String[] array_string = new String[5];
array_string [0] = "1";
array_string [1] = "2";
array_string [2] = "3";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
when i run this code the first element of the array_string i mean (array_string [0] = "1";) value alone displayed in the Spinner when i click spinner for list of items it raising null pointer exception.
You have two null cells in the array (size = 5, set = 3), change this:
String[] array_string = new String[5];
to
String[] array_string = new String[3];
or assign values to array_string[3] and array_string[4]
Well, there are 2 errors:
(1) your 5th line should probably be:
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array_string);
...i.e. array_spinner is not an array you declared in your snippet.
(2) You did not initialize all elements of array_string to an actual string immutable value. This means that array_string[3], array_string[4] are null, therefore raising this null pointer error you speak of.