使用到的jar包:fastjson.jar xUtils.jar;
布局文件:
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" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
===================================================
muti_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="17sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/pic1"
android:layout_margin="2dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="75dp"/>
<ImageView
android:id="@+id/pic2"
android:layout_margin="2dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="75dp"/>
<ImageView
android:id="@+id/pic3"
android:layout_margin="2dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="75dp"/>
</LinearLayout>
</LinearLayout>
====================================================
single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_margin="2dp"
android:layout_width="90dp"
android:layout_height="75dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
</LinearLayout>
==================================================================================
使用到的bean:
1.数据库用使用到的bean
package com.qianfeng.xutiles_text.bean;
public class DbBean {
//属性名作为被创建的表的列名
private String id;//id属性必须有,因为被创建的表是以id列作为主键
private String json;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJson() {
return json;
}
public void setJson(String json) {
this.json = json;
}
}
==================================================
package com.qianfeng.xutiles_text.bean;
public class FeedBean {
private String category;
private String data;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
===============================================================
package com.qianfeng.xutiles_text.bean;
public class PicBean {
private String photo;
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
================================================================================
listView的适配器:
package com.qianfeng.xutiles_text.adapter;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.qianfeng.xutiles_text.R;
import com.qianfeng.xutiles_text.bean.FeedBean;
import com.qianfeng.xutiles_text.bean.PicBean;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FeedAdapter extends BaseAdapter {
private List<FeedBean> dataList;
private Context context;
private String baseUrl = "http://litchiapi.jstv.com";
private BitmapUtils bitmapUtils;
public FeedAdapter(List<FeedBean> dataList, Context context, BitmapUtils bitmapUtils){
this.dataList = dataList;
this.context = context;
this.bitmapUtils = bitmapUtils;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//返回ListView所拥有的item的视图类型的总个数
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
//返回ListView当前项(position项)对应的视图的类型
//视图的类型是以整数表示(注意:表示类型的整数要以0开始)
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
//获取数据集中对应项的数据,根据该数据判定当前item的视图类型
FeedBean bean=dataList.get(position);
String category=bean.getCategory();
if ("map".equals(category)) {
//多图的item
return 0;//定义多图item的视图类型值为0
}else {
return 1;//单图的item,值为0
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//获取当前项的数据
FeedBean bean=dataList.get(position);
String data=bean.getData();
JSONObject dataObject=JSON.parseObject(data);
//判定当前项的item是哪种不同的item布局
int type=getItemViewType(position);
switch (type) {
case 0://多图布局
if (convertView==null) {
convertView=LayoutInflater.from(context).inflate(R.layout.muti_item, null);
MutiViewHolder mutiHolder = new MutiViewHolder();
mutiHolder.subjectText = (TextView) convertView.findViewById(R.id.subject);
mutiHolder.pic1 = (ImageView) convertView.findViewById(R.id.pic1);
mutiHolder.pic2 = (ImageView) convertView.findViewById(R.id.pic2);
mutiHolder.pic3 = (ImageView) convertView.findViewById(R.id.pic3);
convertView.setTag(mutiHolder);
}
MutiViewHolder mutiHolder=(MutiViewHolder) convertView.getTag();
String subject = dataObject.getString("subject");
mutiHolder.subjectText.setText(subject);
JSONArray picsArray = dataObject.getJSONArray("pics");
List<PicBean>picList=JSON.parseArray(picsArray.toString(), PicBean.class);
//使用XUtils框架中的BitmapUtils工具类,实现图片获取和展示
bitmapUtils.display(mutiHolder.pic1, baseUrl + picList.get(0).getPhoto());
bitmapUtils.display(mutiHolder.pic2, baseUrl + picList.get(1).getPhoto());
bitmapUtils.display(mutiHolder.pic3, baseUrl + picList.get(2).getPhoto());
break;
case 1://单图布局
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.single_item, null);
SingleViewHolder singleHolder = new SingleViewHolder();
singleHolder.img = (ImageView) convertView.findViewById(R.id.img);
singleHolder.titleText = (TextView) convertView.findViewById(R.id.title);
singleHolder.contentText = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(singleHolder);
}
SingleViewHolder singleHolder = (SingleViewHolder) convertView.getTag();
String title = dataObject.getString("subject");
String content = dataObject.getString("summary");
String img = dataObject.getString("cover");
singleHolder.titleText.setText(title);
singleHolder.contentText.setText(content);
bitmapUtils.display(singleHolder.img, baseUrl + img);
break;
}
return convertView;
}
class MutiViewHolder{
private TextView subjectText;
private ImageView pic1,pic2,pic3;
}
class SingleViewHolder{
private ImageView img;
private TextView titleText, contentText;
}
}
=============================================================================
package com.qf.gday02_xutils;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.qf.gday02_xutils.adapter.FeedAdapter;
import com.qf.gday02_xutils.bean.DbBean;
import com.qf.gday02_xutils.bean.FeedBean;
public class MainActivity extends Activity {
@ViewInject(R.id.list)//使用XUtils注解(Annotation)标记View
//该注解的作用:告知注解处理器去布局文件中找传入的id的对应的View对象赋值给该属性
private ListView list;
@ViewInject(R.id.btn)
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使用XUtils中的注解处理器,识别注解。
ViewUtils.inject(this);
//根据网络状态判定读取本地还是进行网络请求
ConnectivityManager cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cManager.getActiveNetworkInfo();
if(info != null && info.isAvailable()){//有网
//创建XUtils框架中的HttpUtils网络请求工具类
HttpUtils httpUtils = new HttpUtils();
//使用HttpUtils发起网络请求
String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
//获取请求成功的数据
int statusCode = arg0.statusCode;//获取请求的状态码
if(statusCode == 200){//请求成功数据获取正确
String json = arg0.result;
// Log.v("TAG", "json == " + json);
parseJsonAndShow(json);
}else{
Toast.makeText(MainActivity.this, "数据获取失败", 0).show();
}
}
});
}else{//没网
DbUtils dbUtils = DbUtils.create(this);
try {
DbBean bean = dbUtils.findById(DbBean.class, "1");
String json = bean.getJson();
Log.v("TAG", "本地json === " + json);
//解析本地json
parseJsonAndShow(json);
} catch (DbException e) {
e.printStackTrace();
}
}
}
//解析json并且显示到View中
private void parseJsonAndShow(String json) {
//借助于XUtils的DBUtils实现json字符串的本地缓存
DbUtils dbUtils =DbUtils.create(MainActivity.this);//使用默认参数方式创建数据库
try {
//创建Bean封装被保存的数据
DbBean dbBean = new DbBean();
dbBean.setId("1");
dbBean.setJson(json);
dbUtils.save(dbBean);//将数据插入到表中
} catch (DbException e) {
e.printStackTrace();
}
JSONObject allObject = JSON.parseObject(json);
JSONObject paramzObject = allObject.getJSONObject("paramz");
JSONArray feedsArray = paramzObject.getJSONArray("feeds");
List<FeedBean> feedList = JSON.parseArray(feedsArray.toString(),FeedBean.class);
//把数据传递到适配器中,在适配器中解析每一个item的data数据
BitmapUtils bitmapUtils = new BitmapUtils(MainActivity.this);
//设置BitmapUtils相关参数
//bitmapUtils.configMemoryCacheEnabled(false);//关闭内存缓存
//bitmapUtils.configDiskCacheEnabled(false);//关闭本地缓存
//配置BitmapUtils加载图片的动画
AlphaAnimation animation = new AlphaAnimation(0, 1);
animation.setDuration(3 * 1000);
animation.setFillAfter(true);
// bitmapUtils.configDefaultImageLoadAnimation(animation);
FeedAdapter feeAdapter = new FeedAdapter(feedList, MainActivity.this,bitmapUtils);
list.setAdapter(feeAdapter);
//使用XUtils框架的滚动监听器,控制ListView被滚动的或惯性滚动的时候,BitmapUtils不加载图片
// list.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, true, true));
}
@OnClick(R.id.btn)//使用注解实现为Button绑定点击事件监听器
public void clickButton(View v){
Toast.makeText(this, "click button", 0).show();
}
@OnItemClick(R.id.list)//使用注解实现为ListView绑定项点击事件监听器
public void clickItem(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(this, "点击:" + arg2, 0).show();
}
}