Null Value in songArrayAdapter - android

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

Related

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

Creating a custom hashmapadapter

If I have a hashmap containing the following:
Hashmap contains (String, String)
How can I instantiate a custom adapter? The custom adapter should extend baseadapter.
I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.
Instantiation should look something like this:
MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);
setListAdapter(adapter)
I am lost here so code would be a big help.
THANKS
graham
The following list is using as its datasource a string array called items.
public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items));
however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap
so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm
I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter.
For you example you can do:
HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();
for(int i=0; i<=10;i){
hm.put(i,("num"+i));
}
for (Entry<Integer, String> e : hm.entrySet()) {
String newString=e.toString();
elements.add(newString);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
list.setAdapter(adapter);

Strange behavior with ArrayAdapters in Android

I've been playing around with ArrayAdapters and I've reached a point where I'm getting different results from two almost identical ArrayLists + ArrayAdapter combinations.
The first one:
An ArrayList of 'Restaurant' objects, an ArrayAdapter that uses this ArrayList and a ListView that binds this ArrayAdapter.
private ArrayList<Restaurant> model = new ArrayList<Restaurant>();
private ArrayAdapter<Restaurant> restaurantAdapter = null;
...
public void onCreate(Bundle savedInstanceState){
...
restaurantAdapter = ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
...
listView.setAdapter(restaurantAdapter);
...
}
The second one:
An ArrayList of String objects, an ArrayAdapter that uses this ArrayList and a AutoCompleteTextView that binds this ArrayAdatper.
private ArrayList<String> prevAddressList = new ArrayList<String>();
private ArrayAdapter<String> addListAdapter = null;
...
public void onCreate(Bundle savedInstanceState){
...
addListAdapter = ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, prevAdddressList);
...
autoCompleteField.setAdapter(addListAdapter);
...
}
I have a save button, on click, I'm creating a restaurant object with a name and an address and adding it to the first adapter, additionally, I want to create a list of previously used address so they are "auto completed" next time they are typing it, so I'm taking the text, and adding it to the second adapter.
...onSave = new View.OnClickListener(){
...
restaurantAdapter.add(r); //r is a Restaurant object.
addListAdapter.add(autoCompleteField.getText().toString());
...
}
Now, everything is working properly. I get the Restaurants displayed in a ListView. The AutoComplete is working as expected.... but I noticed something when I was checking the values while debugging:
The actual ArrayLists, model (Restaurant) is getting updated after adding an object to the adapter , but prevAddressList (String) is not.
Unless, I set the AutoCompleteTextField empty.... then, the prevAddressList gets updated after adding something to the second adapter.
Already tried using notifyDataSetChanged(), but it makes no difference (and it is set to true on every adapter by default anyway).
Other behavior that differs between the two adapters is that in the first one (Restaurant), values are going to the mObjects field, while in the second one (String) they are going to mOriginalValues instead.
I'm completely stomped. The only difference between those two adapters is that one is type "Restaurant" and the other is type "String".
Any ideas? Maybe I'm missing something very obvious? Let me know if you need the full code.
thanks
Instead of adding it to the adapter, try adding the object to your list and then calling notifyDataSetChanged on your adapter. The adapter should pick up your changes and your list of course will have the object you just added.
For anyone coming here from google:
Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
This might explain the behavior, although I have to test it myself.

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.

How to update SimpleAdapter in Android

Is it possible to update a SimpleAdapter? I have a list of data and a footer that says "See Next Results" When that list item is clicked I capture the event and get new data. I then want to replace the data in the ListView with this new data but I can't figure out how to do it. Any Ideas? I don't want to use an ArrayAdapter, cause as far as I can see the items can only hold one string where I need it to hold multiple strings and ints.
Update: According to del116, you can indeed give SimpleAdapter a mutable map and then manually call the adapter's notifyDataSetChanged method when you need the list to update. However, my point below stands about the documentation of SimpleAdapter specifying that it is for static data; using it for mutable data is going counter to its design, so if you use this technique I would be sure to check on whether it continues to work in new Android releases as they emerge.
(Original commentary follows:)
If you look at the SimpleAdapter description it says it is "An easy adapter to map static data to views defined in an XML file." I've added the emphasis -- put simply, SimpleAdapater isn't built for use with data that changes; it handles static data only. If you can't use an ArrayAdapter because your data has more than a single bit of text, then you will either have to build your own custom ListAdapter, or put your data in a DB and use one of the CursorAdapters.
As a last resort, if you don't need much performance, you could update a ListView backed by a SimpleAdapter by building a whole new SimpleAdapter instance any time your data changes and telling the list view to use it via setListAdapter.
ListView lv= (ListView) findViewById(R.id.loglist);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "time", "message" };
int[] to = { R.id.logtime, R.id.logmessage };
adapter = new SimpleAdapter(getApplicationContext(), list,R.layout.log_list_row, from,to);
lv.setAdapter(adapter);
Call this function each time to update the ListView. Keep in Mind that You have to update the list variable with new Data..
Hope this Helps..:)
SimpleAdapter is meant for static data, so your performance may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.
I was not able to get notifyDataSetChanged() to work on updating my SimpleAdapter, so instead I tried first removing all views that were attached to the parent layout using removeAllViews(), then adding the ListView, and that worked, allowing me to update the UI:
LinearLayout results = (LinearLayout)findViewById(R.id.results);
ListView lv = new ListView(this);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row,
new String[] { "name", "dept" }, new int[] { R.id.name, R.id.dept } );
for (...) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("dept", dept);
list.add(map);
}
lv.setAdapter(adapter);
results.removeAllViews();
results.addView(lv);

Categories

Resources