android中listview列表的更新问题
就是mars老师的那个MP3播放器例子里面通过选项菜单下载Mp3信息更新到listtview选项那里我更新SDK后就无法通过了好像是说3.0版后不能在主线程上下载然后我又...
就是mars老师的那个MP3播放器例子 里面通过选项菜单下载Mp3信息更新到listtview选项那里 我更新SDK后就无法通过了 好像是说3.0版后不能在主线程上下载 然后我又建了一个线程在线程里面去下载 发现因为线程代码是一起运行的 所以这边XML还没下载完那边就解析了 然后想把解析也放到线程好了。。。 最后发现线程都没有存在意义了。。。。下面是代码。。。
public class Mp3ListActivity extends ListActivity {
private static final int UPDATE = 1;
private static final int ABOUT = 2;
private List<Mp3Info> mp3Infos = null;
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, UPDATE, 1, R.string.mp3list_update);
menu.add(0, ABOUT, 2, R.string.mp3list_about);
return super.onCreateOptionsMenu(menu);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateListView();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == UPDATE) {
updateListView();
} else if (item.getItemId() == ABOUT) {
}
return super.onOptionsItemSelected(item);
}
private void updateListView() {
// 用户点击了更新列表按钮
DLThread dLThread=new DLThread("...urlStr...").start;
String xml=dLThread.getxml;
mp3Infos = parse(xml);
System.out.println(mp3Infos);
SimpleAdapter simpleAdapter = buildSimpleAdapter(mp3Infos);
setListAdapter(simpleAdapter);
}
private String downloadXML(String urlStr) {
HttpDownloader httpDownloader = new HttpDownloader();
String result = httpDownloader.download(urlStr);
System.out.println(result);
return result;
}
class DLThread extends Thread {
String urlStr;
String xml;
DLThread(String urlStr) {
this.urlStr = urlStr;
}
public String getxml() {
return xml;
}
public void run() {
xml = downloadXML(urlStr);
System.out.println(xml);
}
}
} 展开
public class Mp3ListActivity extends ListActivity {
private static final int UPDATE = 1;
private static final int ABOUT = 2;
private List<Mp3Info> mp3Infos = null;
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, UPDATE, 1, R.string.mp3list_update);
menu.add(0, ABOUT, 2, R.string.mp3list_about);
return super.onCreateOptionsMenu(menu);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateListView();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == UPDATE) {
updateListView();
} else if (item.getItemId() == ABOUT) {
}
return super.onOptionsItemSelected(item);
}
private void updateListView() {
// 用户点击了更新列表按钮
DLThread dLThread=new DLThread("...urlStr...").start;
String xml=dLThread.getxml;
mp3Infos = parse(xml);
System.out.println(mp3Infos);
SimpleAdapter simpleAdapter = buildSimpleAdapter(mp3Infos);
setListAdapter(simpleAdapter);
}
private String downloadXML(String urlStr) {
HttpDownloader httpDownloader = new HttpDownloader();
String result = httpDownloader.download(urlStr);
System.out.println(result);
return result;
}
class DLThread extends Thread {
String urlStr;
String xml;
DLThread(String urlStr) {
this.urlStr = urlStr;
}
public String getxml() {
return xml;
}
public void run() {
xml = downloadXML(urlStr);
System.out.println(xml);
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询