Adapter in Android - android

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.

Related

Adding default None value to a spinner from DB cursor

I have a spinner which is populated with DB values returned in form of Cursor.but it always have first value as selected. What I need is to have a "None" value instead od ffirst DB value. I know we can do this when we have hardcoded strings but how can i do it with Cursor..??
My code for spinner -
Cursor c = listNamesDbHelper.fetchAllUserLists();
startManagingCursor(c);
Log.i(TAG, "cursor list names--->>" + c.getCount());
// create an array to specify which fields we want to display
String[] from = new String[c.getCount() + 1];
from = new String[]{listNamesDbHelper.KEY_LIST_NAME};
//from[0] = "None";
// create an array of the display item we want to bind our data to
int[] to = new int[]{R.id.ModifyTaskListNameItem};
// create simple cursor adapter
/*SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.choose_list_modify_task, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );*/
// get reference to our spinner
CustomSpinnerItemsAdapter adapter = new CustomSpinnerItemsAdapter(this,c,from,to);
Spinner s = (Spinner) findViewById( R.id.spinnerListSelection );
s.setAdapter(adapter);
Thanks in advance,
Ray

Convert from string[] to string Android

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

Using Multiple Spinners, But Only One Spinner Has All The Data

I have two spinners and two arrays. However, one spinner receives both arrays while the other receives no values from either of the two arrays. Note: I do not want to use radio buttons as the data is shortened for review.
final ArrayList<String> serialnums = new ArrayList<String>();
serialnums.add("576798");
serialnums.add("495874");
serialnums.add("345667");
serialnums.add("956345");
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
s1 = (Spinner) findViewById(R.id.spinnerSerial);
spinnerCarry = (Spinner) findViewById(R.id.spinnerCarry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, serialnums );
ArrayAdapter<String> adapterCarrys = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carrys );
s1.setAdapter(adapter);
spinnerCarry.setAdapter(adapterCarrys);
Note 2: Spinner s1 gets all the data
One spinner has all the data because you are assigning all the data to it. Change this:
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
to this:
final ArrayList<String> carrys = new ArrayList<String>();
carrys.add("R");
carrys.add("L");
carrys.add("F");
carrys.add("B");
You are adding "R" "L" "F" "B" to serialnums add it to carrys

Dynamic spinner options does not open

I have within a TabActivity a Spinner that will be generated dynamically. Just to test, I did so manually:
Spinner sp_departure = (Spinner) findViewById(R.id.spinner_departure);
// This array will be generated through a database
String[] array_spinner = new String[2];
array_spinner[0] = "Departure 1";
array_spinner[1] = "Departure 2";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_departure.setAdapter(adapter);
When I run the app looks ok since option selected is "Departure 1" but when I click to open the options I get some errors and the application is closed.
Any idea what could be wrong?
Thanks in advance.
- Update
This is what was generated by LogCat: http://pastebin.com/1QPKZdKB
Yes you might have set setContetView(R.layout.yourxml)...,
Change it to :
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.yourxm, null);
this.setContentView(viewToLoad);
and use
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(), android.R.layout.simple_spinner_item, array_spinner);
Try, it may helps you

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