Convert from string[] to string Android - 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};

Related

Android how to trim sharedpreferences name

I'm getting all sharedpreferences created by my app and I insert them in a spinner but every sharedpreferences displayed like "example.xml" etc. I want to trim .xml extension let them just names like "example".
File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");
if(prefsdir.exists() && prefsdir.isDirectory()){
String[] list = prefsdir.list();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,list);
Spinner sp = (Spinner) findViewById(R.id.tum_ilaclar);
sp.setAdapter(adapter);
}
You can use something like this create a new method:
String[] getList(String[] list){
String values[] = new String[list.length];
for(int i=0; i<list.length; i++){
String value = list[i];
values[i]=value.contains(".xml")?value.replace(".xml",""):value;
}
return values;
}
Then in your code change :
String[] list = prefsdir.list();
to
String[] list = getList(prefsdir.list());
This will remove the .xml part.

Selected Spinner Item as String for a new Cursor

Right now I am almost done writing my app. All I need is a bit help on getting the selected String value from a spinner populated from my database by a simple cursor adapter. I am not sure how I can get the String from my spinner and pass it to a different cursor and use the string in a query, that will populate depending on the first choice of the spinner, and so on with other spinners.
Here is the code I am using for one of my spinners.
vType = (Cursor) DataBaseHelper.getPowersportsType();
this.startManagingCursor(vType);
SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vType,
new String [] {DataBaseHelper.POWERSPORTS_TYPE},
new int[] {android.R.id.text1});
scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);
And this is my query for the next spinner in my xml layout
static String MakeWhere = "POWERSPORTS_TYPE=?";
public static Cursor getPowersportsMake(){
return myDataBase.query(POWERSPORTS_TABLE,
new String [] {POWERSPORTS_ID, POWERSPORTS_MAKE},
MakeWhere,
null,
POWERSPORTS_MAKE,
null,
null);
}
Any comments or suggestions are welcomed.
to get the item selected you need to set the onItemSelectedListener
then in your onItemtSelected all you would do is
String selection = vTypeSpinner.getSelectedItem().toString();

Adapter in 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.

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

here is my code for spinner with dropdownlist:

I have spinner in my application .The spinner have drop down list.I want to take the value of the dropdown list from the database .how can i do this ?
here is my code for spinner with dropdownlist:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selectdefault);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
You must create an array to specify which fields you want to display.
You could try this.
DatabaseProfileHelper dbhelper = new DatabaseProfileHelper(context);
// create an array to specify which fields we want to display
String[] from = new String[] { DatabaseProfileHelper.colNama };
int[] to = new int[] {R.id.txtProfileNama};
Cursor c = dbhelper.getAllProfiles();
dbhelper.UpdateProfile(null);
c.moveToFirst();
startManagingCursor(c);
// create simple cursor adapter
SimpleCursorAdapter ca=new SimpleCursorAdapter(context,R.layout.profile_spinner_row, c, from, to);
// get reference to our spinner
spinProfile.setAdapter(ca);

Categories

Resources