Search code examples
androidhashmapstring-concatenationsimpleadapter

Android HashMap and SimpleAdapter - concatenating strings


I have a hashmap that I am loading into a simple adapter

public static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String,    String>>();

....
    musiclist = (TextView) getView().findViewById(R.id.checkTextView);
    filelv = (ListView)getView().findViewById(R.id.FileList);
    filelv.setItemsCanFocus(false);
    filelv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

    //if (trackcount>previousTrackCount){
    ListAdapter playlistadapter = new SimpleAdapter(getActivity().getApplicationContext(), songsList, R.layout.file_view,
                new String[] { "songTitle","songAlbum", "songPath" }, new int[] { R.id.checkTextView, R.id.text2, R.id.text3 });
    filelv.setAdapter(playlistadapter);

This works perfectly, giving me all three values displayed in my listview.

However... I want to be able to concatenate strings so that the second row of the each list item is as follows

songAlbum(songArtist)

e.g. There is Noting Left to Lose (Foo Fighters)

When i change the ListAdapter to the following, the second row is just blank

ListAdapter playlistadapter = new SimpleAdapter(getActivity().getApplicationContext(), songsList, R.layout.file_view,
                new String[] { "songTitle","songAlbum" + "(" + "songArtist" + ")", "songPath" }, new int[] { R.id.checkTextView, R.id.text2, R.id.text3 });

I have test all strings to ensure that they contain data.

Suggestions please!


Solution

  • You will have to add another textbox. And use below code.. offcourse you will have tohave a hashmap with all 4 keys.

    SimpleAdapter playlistadapter = new SimpleAdapter(getActivity().getApplicationContext(), songsList, R.layout.file_view,
                    new String[] { "songTitle","songAlbum", "songartist","songPath" }, new int[] { R.id.checkTextView, R.id.text2, R.id.text3, R.id.text4 });
    
    playlistadapter.setViewBinder(new SimpleAdapter.ViewBinder(){
        boolean setViewValue(View view, Object data, String textRepresentation){
                if(view.getId () == R.id.songartist){
                    view.setText("("+textRepresentation+")");
                }
        }
    });