快取操作流程邏輯
阿新 • • 發佈:2019-01-04
以載入圖文資料的listview:
要儲存的資料:json資料+圖片資料(bitmap)
1、判斷快取中是否存在該快取json資料
// 判斷快取中是否有資料
JSONArray categorydata =
(JSONArray) mCache.getAsJSONArray("categoryData");
(1)、若是不存在,則載入新的:
else {
RequestParams params5 = new RequestParams();
params5.addQueryStringParameter("id" , faId_index);
http.send(HttpMethod.POST, Config.getCatatoryList, params5,
new RequestCallBack<String>() {
@Override
public void onStart() {
}
@Override
public void onLoading(long total, long current,
boolean isUploading) {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
try {
JSONArray jsonarr = new JSONArray(
responseInfo.result);
// 儲存json資料到快取中
mCache.put("categoryData", jsonarr);
// 指定非同步載入資料
Message msg = new Message();
msg.obj = jsonarr;
msg.what = 1;
handler.sendMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(HttpException error, String msg) {
}
});
}
—->非同步操作:
// 非同步操作
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
try {
JSONArray jsonarr = (JSONArray) msg.obj;
int dd_count = jsonarr.length();
list.clear();
for (int i = 0; i < dd_count; i++) {
JSONObject jsonob = jsonarr.getJSONObject(i);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("pic", jsonob.get("imgUrl_small"));
map.put("title1", jsonob.get("name"));
map.put("title2", jsonob.get("desc"));
map.put("categoryids", jsonob.get("id"));
list.add(map);
}
if (msg.what == 1) {
myCatergorAdapter = new CatergoryAdapter(
CategoryActivity.this, list, mCache, 1);
} else if (msg.what == 2) {
myCatergorAdapter = new CatergoryAdapter(
CategoryActivity.this, list, mCache, 2);
}
} catch (Exception ex) {
} finally {
catergory_listview.setAdapter(myCatergorAdapter);
catergory_listview
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterview,
View view, int parent, long id) {
try {
Intent intent = new Intent(
CategoryActivity.this,
CategoryprodshowActivity.class);
intent.putExtra("typeId", String
.valueOf(list.get(parent).get(
"categoryids")));
startActivity(intent);
} catch (Exception ex) {
}
}
});
}
};
};
—–》實現介面卡:載入資料(快取和網路)
@SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
layoutInflater = LayoutInflater.from(context);
// 組裝資料
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.activity_category_item, null);
holder.image = (ImageView) convertView
.findViewById(R.id.catergory_image);
holder.title = (TextView) convertView
.findViewById(R.id.catergoryitem_title);
holder.content = (TextView) convertView
.findViewById(R.id.catergoryitem_content);
holder.goto_btn = (ImageView) convertView
.findViewById(R.id.goto_btn);
// 使用tag儲存資料
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(aalist.get(position).get("title1").toString());
holder.content.setText(aalist.get(position).get("title2")
.toString());
final String img_url2 = aalist.get(position).get("pic").toString();
//判斷載入的是網路圖片還是快取的圖片
if(itemtype==1){
final String img_url = Config.url_header2+aalist.get(position).get("pic").toString();
BitmapUtils bitmap = new BitmapUtils(context);
bitmap.display(holder.image, img_url);
//資料快取操作
new Thread() {
public void run() {
// 圖片資源
//String url = "http://start.firefoxchina.cn/img/worldindex/logo.png";
// 得到可用的圖片
Bitmap bitmap = ImgUtils.getHttpBitmap(img_url);//getHttpBitmap(url);
Bundle b = new Bundle();
b.putString("img_url2", img_url2);
Message msg=new Message();
msg.setData(b);
msg.what=1;
msg.obj=bitmap;
handler.sendMessage(msg);
// imageView.setBackgroundResource(R.drawable.ic_launcher);
};
}.start();
}else if(itemtype==2){
Bitmap bitcache=mCache.getAsBitmap(img_url2);
holder.image.setImageBitmap(bitcache);
}
return convertView;
}
public class ViewHolder {
ImageView image;
TextView title;
TextView content;
ImageView goto_btn;
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int s = msg.what;
if(s==1){
Bundle b=msg.getData();
Bitmap bits=(Bitmap) msg.obj;
String img_url3=(String) b.get("img_url2");
mCache.put(img_url3, bits);
}
}
};
(2)、若是存在快取,則載入快取資料:
if (categorydata != null) {
// 指定非同步載入資料
Message msg = new Message();
msg.obj = categorydata;
msg.what = 2;
handler.sendMessage(msg);
}