I have several spinners in a class.
For the first spinner I have set of data.
Other spiners will download data from server according to the selection of the fist spinner.
However, after downloading data, it does not update the spinner adapters.
Adapter for the second spinner:
sectionField = new String[] {"Error"};
adapterSection = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, sectionField);
adapterSection
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
section.setAdapter(adapterSection);
Code sample from fisrts spinner onItemSelected, which I believe should update the adapter:
sectionField = new String[data.length()];
sectionField = data;
section.setVisibility(View.VISIBLE);
adapterSection.notifyDataSetChanged();
You need to clean your adapter, then add items and notify.
adapterSection.clear();
adapterSection.addAll(data);
adapterSection.notifyDataSetChanged();
Hope it's help.
Related
I have made various string-arrays in the string.xml file and I have to set different arrays as entries for the spinner according to certain condition in Java. Is it possible or is database the only way to do so. Thanks in advance.
You need to use an adapter and populate with tha array in xml file.
Specify the name of your array in xml at createFromResource method (second parameter).
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.my_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
You have to extract your data from file:
String[] testArray = getResources().getStringArray(R.array.testArray);
Then, you have to inflate in the spinner:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, testArray );
mySpinner.setAdapter(spinnerArrayAdapter);
You can start with using ArrayAdapter, it is a simple class to populate spinner items programmatically.
String data[];
//... do your stuff to get populate this array
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, data);
mySpinner.setAdapter(spinnerArrayAdapter);
You can also modify the view of dropdown items and customize them further by overriding this class.
I have an android app that lists connected clients in a listview, but whenever someone connects, it just adds them to the same line, this is the code I use to add connected client. I am new to listview, and not sure how to do this properly, I looked at the android docs but hard to say what needs to be used. If anyone can help me out that would be great.
remoteip += socket.getInetAddress();
ArrayList<String> addclientlist = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
addclientlist.add(remoteip);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter)
I think you need to use this constructor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this, android.R.layout.simple_list_item_1, addclientlist);
Also no need to set adapter always just set it once and each time you have to add new item to adapter you can use either of these as in java Array objects are pass by reference, and then call notifydatasetchanged
adapter.add(remoteip);
//or addclientlist.add(remoteip);
adapter.notifyDataSetChanged(); // Dont forget this
You are updating the adapter initialization list, which is futile.
Instead - update the actual adapter:
adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
listview.setAdapter(adapter)
...........
adapter.add(remoteip); // <----- instead of addclientlist.add()
adapter.notifyDataSetChanged();
need to do so when you click on a specific button (application type translator and arrow key) changed one of the selected items to another spinner itam swapped Well, here is how it can be implemented?
here is the code but it does not work!
int spinner1Index = spinner.getSelectedItemPosition();
spinner.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinnerfirst);
can offer something similar or fix!
You would have to change the adapter of your spinner and call notifyDataSetChanged()
I would suggest adding this to your switch case for when the item is selected.
Set a custom adapter for the spinner on item click.
//An array of what you want to populate the spinner with
Array spinnerArray [] = {"One", "Two", "Etc.."};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//Before setting the adapter, clear and notify the previous ArrayList used
arrayList.clear();
arrayList.notify();
spinner.setAdapter(spinnerArrayAdapter);
I have a XML File and I had parsed the data into the textView successfully now I want to bind that data into the ArrayList or List and display it in ListView.
But I don't know how to bind arraylist data into the ListView.
I have added all data into the arraylist successfully as mentioned in the below code.
List al = new ArrayList();
al.add(parser.getAttributeValue(null, "firstnames"));
Kindly please help me with the code syntax for the above issue.
Regards .
Thanks in advace
please have a look at the sample at http://codinglookseasy.blogspot.in/2012/07/android-list-view-sample.html
Instead of this
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, months);
setListAdapter(aa);
use this in your case
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
setListAdapter(aa);
You need to use an Adapter to bind a List to a ListView, like this:
List<String> list = new ArrayList<String>();
// add data to list
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
Notice that the subtype of List (which is String) matches the subtype of the ArrayAdapter (also String). The layout android.R.layout.simple_list_item_1 defines how the String is displayed in every row. You can look up the specifics of this layout in your SDK, if you want you can also use you own layout. Hope that helps, good luck learning Android!
I have spinner with array list thats work fine, but i want to sort out the datas from a to z (example: apple,ball,cat,dog...)order. I submit my code below
ArrayList<String> SourceArray = new ArrayList<String>();
Spinner Sourcespinner;// = new Spinner(this);
Sourcespinner = (Spinner)findViewById(R.id.Spinner1);
ArrayAdapter<String> SourceArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,SourceArray);
SourceArrayAdapter.add("Chennai");
SourceArrayAdapter.add("Mumbai");
SourceArrayAdapter.add("Kolkatta");
SourceArrayAdapter.add("Delhi");
Sourcespinner.setAdapter(SourceArrayAdapter);`
I don't know how to do sorting for this
you can use this to sort your data
Collections.sort(SourceArray);
Try to add data to the ArrayList and just use the Collections class to sort for you:
Collections.sort(SourceArray);
If you need to add your own objects they need to implement the Comparable interface and implement the method compareTo(). When changing the ArrayList's data make sure to notify the adapter that new data might have been added by using this code:
SourceArrayAdapter.notifyDataSetChanged();