2019独角兽企业重金招聘Python工程师标准>>>
ListView和SimPleteAdapter 把新闻数据绑定到ListView
//布局界面有2个xml文件 1 -- ListView -- activity_main.xml
2 -- 新闻数据内容 -- item_activity.xml
//记得添加联网权限
1、activity_main.xml文件 布局
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
---------------
item_activity.xml文件 布局
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"/>
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"/>
<TextView
android:id="@+id/changed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"/>
</LinearLayout>
----------------------
2、MainActivity 类
代码
public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview = (ListView) this.findViewById(R.id.listview);
String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
new MyAsyncTask().execute(url);
}
//启动工具类 进行联网下载数据操作
class MyAsyncTask extends AsyncTask<String, Void, byte[]>{
@Override
protected byte[] doInBackground(String... params) {
String url = params[0];
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode() == 200){
return EntityUtils.toByteArray(response.getEntity());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] result) {
if(result != null){
//把下载好的数据用json解析 把需要的数据解析出来 方法 -- jsonObject
List<Map<String, Object>> data = jsonObject(new String(result));
//把需要的数据解析好 返回来 然后 装在一个集合里
//把装在集合里的数据丢到自定义的适配器里
adapter = new SimpleAdapter(MainActivity.this, data , R.layout.item_activity,
new String[]{"subject","summary","changed"}, new int[]{R.id.subject,R.id.summary,R.id.changed});
//把适配器绑定到ListView
listview.setAdapter(adapter);
}
}
private List<Map<String, Object>> jsonObject(String string) {
List<Map<String, Object>> data_list = new ArrayList<Map<String, Object>>();
try {
JSONObject obj = new JSONObject(string);
JSONObject obj_paramz = obj.getJSONObject("paramz");
JSONArray array_feeds = obj_paramz.getJSONArray("feeds");
for(int i = 0;i<array_feeds.length();i++){
JSONObject object = array_feeds.getJSONObject(i);
JSONObject obj_data = object.getJSONObject("data");
Map<String,Object> map = new HashMap<String, Object>();
map.put("subject", obj_data.getString("subject"));
map.put("summary", obj_data.getString("summary"));
map.put("changed", obj_data.getString("changed"));
data_list.add(map);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data_list;
}
}
}