刚刚入门学习了Android的ListView,也是看了传智播客黎活明老师的视频学习了,但是一直运行不了,还报错了。报的错误是:at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1......省略后面的了。。。。。后面上网查找了一下资料,原因是Android4.0以后访问网络要在子线程里执行;不能在主程序里执行。查找了一下资料对这个例子进行了改进,下面链接的例子代码就是视频里面的差不多的代码:http://blog.csdn.net/lynxzong/article/details/12435451
下面的是我在这个例子上改过的代码,只是改动了MainActivity而已,其他的跟上面链接的代码一样的。希望各位多多指导;有写错或者写不好的地方求改正。。。
package com.example.videonews;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.domain.News;
import com.example.service.VideoNewsService;
import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Context context;
private ListView listview;
private ProgressDialog dialog;
private SimpleAdapter adapter;
private VideoNewsService videoNewsService;
private List<News> videosList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
/**初始化界面**/
initView();
/**加载基本的控件,并设置加载时候的滚动条**/
dialog = new ProgressDialog(context);
dialog.setMessage("正在加载数据...");
dialog.setCancelable(false);
dialog.show();
Thread thread=new Thread(new loadListRunnable());
thread.start();
}
/**
* *初始化界面
*/
private void initView() {
listview=(ListView) findViewById(R.id.listview);
}
public class loadListRunnable implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
videoNewsService=new VideoNewsService();
try {
//videosList=videoNewsService.getLastNews();//读取xml文件
videosList=videoNewsService.getJSONLastNews(); //读取json
Bundle bundle=new Bundle();
if(null!=videosList && videosList.size()>0)
{
bundle.putString("result", "success");
Message message=new Message();
message.setData(bundle);
handler.sendMessage(message);
}
else
{
bundle.putString("result", "fail");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 对返回的数据进行处理
*/
private Handler handler=new Handler(){
public void handleMessage(Message msg)
{
String msStr=msg.getData().getString("result");
if("success".equals(msStr))
{
//加载数据赋值
initListView();
//数据加载完成后结束进度条
dialog.dismiss();
}
else{
Toast.makeText(getApplicationContext(), "获取数据失败", 0).show();
}
}
};
protected void initListView() {
List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
for(News news:videosList)
{
HashMap<String, Object> item=new HashMap<String, Object>();
item.put("id", news.getId());
item.put("title", news.getTitle());
item.put("timelength", getResources().getString(R.string.timelength)+
news.getTimelength()+getResources().getString(R.string.fenzhong));
data.add(item);
}
SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.items,
new String[]{"title","timelength"}, new int[]{R.id.title,R.id.timelength});
listview.setAdapter(adapter);
}
}