Creating a custom hashmapadapter - android

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

Related

Programatically replace String Array in a ListView Array Adapter

I have a ListView that i have populated with a string array: {"1","2","3"} using an Array Adapter.
(ListView) ListOptions = (ListView)findViewById(R.id.ListOptions);
String[] exampleString = new String[]{"1","2","3"};
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,exampleString);
ListOptions.setAdapter(adapter);
Later on, i want to be able to change exampleString to exampleString2:
{"4","5","6"}
and have the ListView update with exampleString2 displayed as the list options.
is there something i can do along the lines of:
adapter.ChangeStringArray(exampleString2);
ListView.setAdapter(adapter);
I want to avoid having to create a new adapter each time I change the String array that is populating the list items.
ArrayAdapter has methods for managing the backing data array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>());
adapter.addAll(exampleString);
adapter.clear();
adapter.addAll(exampleString2);
adapter.notifyDataSetChanged();

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

Android ListView: don't show all strings

I show a listview from the following string:
String[] values = new String[] { test1, test2, test3 };
The variables:
private String test1 = "test";
private String test2 = "test";
private String test3 = "test3";
Now I don't want to show the strings that contain "test" in my listview.
Like this:
if (String == "test") {
*don't show in ListView*;}
And I want it to test all Strings at once if they contain "test"
How is it possible?
EDIT: Here the adapter code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
Yup...
if (values[i].equals("test")) // i being the iterator of your for-loop
OR
if (values[i].contains("test"))
contains is a slow(er) string function though.
You might want to use an ArrayList though... that way you can just add all those objects to the array list, then iterate through it... and remove them as you go...
// Do something to add all items to your array list
...
// Iterate through the list, removing what doesn't need to be there
for (int i = 0; i < arrayList.size(); i++){
if (arrayList.get(i).contains("test"))
arrayList.remove(i);
}
...Then set 'arrayList' (or whatever you've called it) as the string list for your adapter. If you use the same constructor it'll look like this...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrayList.toArray());
all the time, if you want compare 2 strings use the equals methode
read this about The equals() method
You should be using a filter on your ListView. Look at these examples:
(Simple iterating) http://androidsearchfilterlistview.blogspot.com/2011/06/android-custom-list-view-filter.html
(getFilter()) Filtering ListView with custom (object) adapter
Like others have posted, you compare String objects using .equals(), but if you are trying to only display certain items in your ListView you should use the getFilter() method like described in the link I posted.
edit: I found you a nice SO example.

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