问题描述
- 大四安卓学习mars播放器Listview的问题
-
抱歉麻烦各位了,本人大四狗,目前在看安卓开发,学习mars老师的音乐播放器时遇到了
个问题,就是设置了SimpleAdapter之后仍然无法显示歌名的内容,只显示了个空白的
TEXTVIEW,我研究了无数次依旧没有发现问题在哪,因此来求教各位大神
附上全部代码,没有错误信息,就是显示不出来,每一步调试过确定都有值
代码如下package com.example.android_mp3; import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import com.download.HttpDownloader; import com.model.Mp3Info; import com.xml.Mp3ListContentHandler; import android.app.ListActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends ListActivity { private static final int UPDATE = 1; private static final int ABOUT = 2; private List<Mp3Info> mp3Infos = null; /* * 用户点击menu按钮后调用,可加入自己的按钮控件 */ @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO 自动生成的方法存根 // 组id,物品id,排序,显示内容 menu.add(0, UPDATE, 1, R.string.mp3list_update); menu.addSubMenu(0, ABOUT, 2, R.string.mp3list_about); return super.onCreateOptionsMenu(menu); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(r).start(); // updateListView(); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == UPDATE) { } else if (item.getItemId() == ABOUT) { System.out.println("1"); // updateListView(); } return super.onOptionsItemSelected(item); } // public void updateListView() { // new Thread(new Runnable() { // // @Override // public void run() { // // TODO 自动生成的方法存根 // String xml = downloadXML("http://192.168.88.100:8080/mp3/resources.xml"); // System.out.println("XML------>"); // System.out.println(xml); // System.out.println(Thread.currentThread().getName()); // mp3Infos = parse(xml); // Message msg = Message.obtain(); // msg.sendToTarget(); // } // }) .start(); // List<HashMap<String, String>> list = new ArrayList<HashMap<String, // String>>(); // for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) { // Mp3Info mp3Info = (Mp3Info) iterator.next(); // HashMap<String, String> map = new HashMap<String, String>(); // map.put("mp3_name", mp3Info.getMp3Name()); // map.put("mp3_size", mp3Info.getMp3Size()); // list.add(map); // } // SimpleAdapter adapter = new SimpleAdapter(this, list, // R.layout.mp3info_xml, new String[] { "mp3_name", "mp3_size" }, // new int[] { R.id.mp3_name, R.id.mp3_size }); // setListAdapter(adapter); // } private Handler handler = new Handler() { public void handleMessage(Message msg) { // 显示列表 showList(); } }; Runnable r = new Runnable() { public void run() { String xml = downloadXML("http://192.168.88.100:8080/mp3/resources.xml"); System.out.println("XML------>"); System.out.println(xml); // 解析下载的XML文件 mp3Infos = parse(xml); Message msg = handler.obtainMessage(); msg.sendToTarget(); } }; public void showList() { List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) { Mp3Info mp3Info = (Mp3Info) iterator.next(); HashMap<String, String> map = new HashMap<String, String>(); map.put("mp3_name", mp3Info.getMp3Name()); map.put("mp3_size", mp3Info.getMp3Size()); list.add(map); } SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list, R.layout.mp3info_xml, new String[] { "mp3Name", "mp3Size" }, new int[] { R.id.mp3_name, R.id.mp3_size }); setListAdapter(simpleAdapter); } private String downloadXML(String urlString) { HttpDownloader httpDownloader = new HttpDownloader(); String result = httpDownloader.download(urlString); return result; } private List<Mp3Info> parse(String xmlStr) { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); List<Mp3Info> infos = new ArrayList<Mp3Info>(); try { XMLReader xmlReader = saxParserFactory.newSAXParser() .getXMLReader(); Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler( infos); xmlReader.setContentHandler(mp3ListContentHandler); xmlReader.parse(new InputSource(new StringReader(xmlStr))); for (Iterator iterator = infos.iterator(); iterator.hasNext();) { Mp3Info mp3Info = (Mp3Info) iterator.next(); System.out.println(mp3Info); } } catch (SAXException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return infos; } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // Mp3Info mp3Info = mp3Infos.get(position); // System.out.println("----->mp3info" + mp3Info); super.onListItemClick(l, v, position, id); } }
xml的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="1dip" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip"> <TextView android:id="@+id/mp3_name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="10pt" /> <TextView android:id="@+id/mp3_size" android:layout_width="180dip" android:layout_height="30dip" android:textSize="10pt" /> </LinearLayout>
解决方案
你的代码贴全了吗?
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();
mp3Infos 是什么?是音乐数据的集合吗?把错误贴出来看看
解决方案二:
只用一个引用试试,在上面HashMap map=null;
下面map=new HashMap
时间: 2024-10-26 21:49:29