Spinner data sorting in Android - 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();

Related

Spinner appears empty but contains data

I'm having an issue with my spinner. When I debug, I can see that there is data : my adapter contains objects.
Debug spinner adapter screenshot
However, the spinner appears like it has nothing in it :
My empty spinner
Here is the (supposed) interesting code :
`
ArrayList<ActivityToSteps> activityConversionList;
ArrayList<String> activityList;
ArrayAdapter<String> categories_adapter;
activityConversionList = new ArrayList<>();
activityList = new ArrayList<>();
categories_adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, activityList);
categories_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
activityConversionList.addAll((ArrayList<ActivityToSteps>)task.getResult());
for(ActivityToSteps ats : activityConversionList){
activityList.add(ats.getActivityName());
}
categories_adapter.notifyDataSetChanged();
`
ActivityToSteps is a class I created, containing a String "ActivityName" attribute and a float "StepsPerMin" attribute.
I'm getting a list of ActivityToSteps from an async task, and I want a spinner containing all their "ActivityName", stored in the ArrayList "activityList" (which is not empty, but can't show screenshot beacause reputation < 10...).
I hope I've been clear enough!
Thanks in advance for your time!!
Please double check that you have passed your list to spinner adapter.
your_categories_spinner.setAdapter(categories_adapter);
just forgot to set the adapter to the spinner..
spinner.setAdapter(categories_adapter);

Null Value in songArrayAdapter

s1="http://newmp3mad.com/128-734892s/Manali%20Trance.mp3";
s2="http://d.djtune.net/data/96/Sajan_Main_Haari-Harshdeep_Kaur%5Bwww.Mp3MaD.Com%5D.mp3";
s3="http://d.djtune.net/dataa/731660a/Sawan_Aaya_Hai_(Creature_3D)-Arijit_Singh%5Bwww.Mp3MaD.Com%5D.mp3";
s4="http://d.djtune.net/dataa/45695/Chittiyaan_Kalaiyaan-Meet_Bros_Anjan_Ankit%5Bwww.Mp3MaD.Com%5D.mp3";
s5="http://d.djtune.net/dataa/736379u/Saanson_Ko-Arijit_Singh%5Bwww.Mp3MaD.Com%5D.mp3";
String[]songUrllist={s1,s2,s3,s4,s5};
playpauseButton=(Button)findViewById(R.id.play_pause);
songSeekBar=(SeekBar)findViewById(R.id.seekBar);
String[]songNamelist={"Manali Trance","Sajan Main Haari","Saawan Aaya Hai","Chittiyaan Kalaiyaan","Saanson Ko"};
songArrayList=new ArrayList<>();
for(int i=0;i<5;i++){
urlHashMap=new HashMap<>();
urlHashMap.put("URL",songUrllist[i]);
urlHashMap.put("NAME",songNamelist[i]);
songArrayList.add(urlHashMap);
}
songArrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,songArrayList);
songSpinner.setAdapter(songArrayAdapter);
songSpinner.setOnItemClickListener(this);
You are not using a List<String> as input data to the ArrayAdapter. The basic ArrayAdapter has no way of handling your list of HashMap<> You need to extend the ArrayAdapter with a own class that handles your specific data.
Nathan has written a good example on Using an ArrayAdapter with ListView

How to update adapter?

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.

listview not updating with notifydatasetchanged() call

This is my code
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
adapter = new CustomAdap(this, temp);
listview.setAdapter(adapter);
The above code works fine.But when i change my code to this
listview =(ListView) findViewById(R.id.lv1);
adapter = new CustomAdap(this, temp);
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
Listview does not show anything.what is the problem?
It looks like you're changing the collection that you initialized adapter with. I would change your code in this way:
// initial setup
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
adapter = new CustomAdap(this, Monday);
listview.setAdapter(adapter);
// change your model Monday here, since it is what the adapter is observing
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Note that if your CustomAdap was a subclass of ArrayAdapter, you could also have done
// change your array adapter here
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
adapter.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
EDIT: I understand more what you want to do now thanks to your comment. You'll probably want to have the adapter replace its contents with that your different ArrayLists then. I would make your CustomAdap be a subclass of ArrayAdapter.
Then you can utilize it this way:
// replace the array adapters contents with the ArrayList corresponding to the day
adapter.clear();
adapter.addAll(MONDAY);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Why it works in first code ?
--- Because you are setting the values to temp List and passing it the adapter and it shows it into listview.
Why not work in second code ?
--- Because you are setting temp to adapter far before you set value into temp
second,your adapter class might not getting the updated value when you set new value to temp ..that because temp is not public or not at class level or not static..
Put the temp declaration at root level and try.
And please show your full code as much as required and Logcat if you getting any warnings than also.
Check for a link to your referenced view in the proper xml file. Or at least check for the existence of said xml file.
What adapter are you using? It is clearly a case where your adapter is not getting updated after u set the data in your temp variable.

How do you add array to listview

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Categories

Resources