简体   繁体   English

从哈希映射的ArrayList检索数据在Android中始终显示为null

[英]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. 我正在尝试从HashMap的数组列表中检索数据,但它始终显示为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... :( 我在添加songList后尝试了以下代码,它始终为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" . 从您发布的代码中无法得知,但是我假设KEY_TITLE常量的值实际上是"title"而不是"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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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