Question

I am trying to retrieve data from an arraylist of HashMap but it is always showing null. Strange the listview is showing up the data perfectly.

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


 //Count from the server 
 int count = dataCount();

 for (int i = 0; i < dataCount; i++) 
{
HashMap<String, String> map = new HashMap<String, String>();

// adding data to HashMap key => value
map.put(KEY_ID, trackNumber);
map.put(KEY_TITLE, trackTitle);
map.put(KEY_ARTIST, trackArtist);
map.put(KEY_DURATION, trackDuration);
map.put(KEY_THUMB_URL, trackAlbumArt);

// adding HashList to ArrayList
songsList.add(map);

}

I tried the below code after adding the songList it is always null... :(

  Log.e("Title","1"+songsList.get(0).get("KEY_TITLE"));

Setting the adapter here:

adapter=new LazyAdapter(this, songsList);        
list.setAdapter(adapter);
Was it helpful?

Solution

Change this

 Log.e("Title","1"+songsList.get(0).get("KEY_TITLE"));

to

 Log.e("Title","1"+songsList.get(0).get(KEY_TITLE));

No way of telling from the code you posted, but I assume that the value of the KEY_TITLE constant is actually "title" instead of "KEY_TITLE".

OTHER TIPS

This is because you'r trying to fetch your data with this

 Log.e("Title","1"+songsList.get(0).get("KEY_TITLE"));

But you have may be already defined your string in declaration part. So above code gives you always null. So just change

 Log.e("Title","1"+songsList.get(0).get(KEY_TITLE));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top