简体   繁体   中英

Retrieve Data from ArrayList of hash map show null always in Android

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

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" .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM