转载地址:http://www.2cto.com/kf/201502/376042.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
/**
* 缓存json数据
*/
private
LruCache<integer, string=
""
> mJsonCache;
/**
* 缓存图片信息
*/
private
LruCache<integer, bitmap=
""
> mBitmapCache;
public
Util() {
mJsonCache =
new
LruCache<integer, string=
""
>(
1
*
1024
*
1024
);
mBitmapCache =
new
LruCache<integer, bitmap=
""
>(
2
*
1024
*
1024
);
}
/**
* 添加进入缓存列表
*
* @param key
* @param value
*/
public
void
addJsonLruCache(Integer key, String value) {
mJsonCache.put(key, value);
}
public
void
addBitmapLruCache(Integer key, Bitmap value) {
mBitmapCache.put(key, value);
}
/**
* 从缓存列表中拿出来
*
* @param key
* @return
*/
public
String getJsonLruCache(Integer key) {
return
mJsonCache.get(key);
}
public
Bitmap getBitmapLruCache(Integer key) {
return
mBitmapCache.get(key);
}</integer,></integer,></integer,></integer,>
|
1
2
3
|
// 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。
// LruCache通过构造函数传入缓存值,以KB为单位。
int
maxMemory = (
int
) (Runtime.getRuntime().maxMemory() /
1024
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
class
MainActivity
extends
Activity
implements
OnItemClickListener {
private
static
final
String LIST_DATA = http:
//api.yi18.net/top/list;
private
ListView mListView;
private
ArrayAdapter<string> mAdapter;
private
ArrayList<integer> mListId;
private
Util util;
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
util =
new
Util();
mListView = (ListView) findViewById(R.id.list);
mListId =
new
ArrayList<integer>();
mAdapter =
new
ArrayAdapter<string>(
this
,
android.R.layout.simple_list_item_1);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(
this
);
new
DownLoadJson().execute(LIST_DATA);
}</string></integer></integer></string>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
private
void
getJsonData(String json) {
try
{
JSONObject jsonObject =
new
JSONObject(json);
if
(jsonObject.getBoolean(success)) {
JSONArray jsonArray = jsonObject.getJSONArray(yi18);
for
(
int
i =
0
; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
if
(i <
5
) {
mAdapter.add(jsonObject2.getString(title));
mListId.add(jsonObject2.getInt(id));
}
}
}
}
catch
(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class
DownLoadJson
extends
AsyncTask<string, string=
""
> {
@Override
protected
String doInBackground(String... params) {
return
util.downLoadJson(params[
0
]);
}
@Override
protected
void
onPostExecute(String result) {
if
(result !=
null
) {
getJsonData(result);
}
}
}</string,>
|
我们就简单的取了前五条数据用来模拟我们的新闻,用的是热点热词的Api。
3,缓存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Override
public
void
onItemClick(AdapterView<!--?--> arg0, View arg1,
int
arg2,
long
arg3) {
// TODO Auto-generated method stub
String message = util.getJsonLruCache(mListId.get(arg2));
Bitmap bitmap = util.getBitmapLruCache(mListId.get(arg2));
if
(message !=
null
) {
intentNewsInfo(arg2, message, bitmap);
}
else
{
intentNewsInfo(arg2,
null
,
null
);
}
}
public
void
intentNewsInfo(
int
arg2, String message, Bitmap bitmap) {
Intent intent =
new
Intent(MainActivity.
this
, NewsinfoActivity.
class
);
intent.putExtra(message, message);
intent.putExtra(bitmap, bitmap);
intent.putExtra(index, arg2);
intent.putExtra(id, mListId.get(arg2));
startActivityForResult(intent,
100
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
public
class
NewsinfoActivity
extends
Activity {
private
String NEWS_INFO = http:
//api.yi18.net/top/show?id=;
private
String imageRes[] = {
http:
//d.hiphotos.baidu.com/image/h%3D360/sign=405b763459afa40f23c6c8db9b65038c/562c11dfa9ec8a13508c96e6f403918fa0ecc026.jpg,
http:
//c.hiphotos.baidu.com/image/h%3D360/sign=798b4f82caea15ce5eeee60f86013a25/9c16fdfaaf51f3dece3f986397eef01f3a297923.jpg,
http:
//f.hiphotos.baidu.com/image/h%3D360/sign=20a94e03940a304e4d22a6fce1c9a7c3/ac4bd11373f082028719ab3848fbfbedab641b29.jpg,
http:
//b.hiphotos.baidu.com/image/h%3D360/sign=3a1af7349145d688bc02b4a294c37dab/4b90f603738da977c0f5b82cb351f8198718e3db.jpg,
http:
//d.hiphotos.baidu.com/image/h%3D360/sign=75e596560f33874483c5297a610ed937/55e736d12f2eb9381891b2f4d6628535e5dd6f3c.jpg };
private
Intent intent;
private
Util util;
private
int
newId, index;
private
ImageView imageView;
private
TextView textView;
private
Bitmap bitmap;
private
String message;
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsinfo);
intent = getIntent();
util =
new
Util();
imageView = (ImageView) findViewById(R.id.image);
textView = (TextView) findViewById(R.id.message);
newId = intent.getExtras().getInt(id);
index = intent.getExtras().getInt(index);
if
(intent.getExtras().getString(message) !=
null
) {
message = intent.getExtras().getString(message);
bitmap = intent.getParcelableExtra(bitmap);
textView.setText(Html.fromHtml(message));
imageView.setImageBitmap(bitmap);
Toast.makeText(
this
, 没有访问网络哦,
2000
).show();
}
else
{
new
DownLoadJson().execute(NEWS_INFO + newId);
new
DownLoadBitmap().execute(imageRes[index]);
Toast.makeText(
this
, 访问网络哦,
2000
).show();
}
}
@Override
public
void
onBackPressed() {
Intent dataIntent =
new
Intent();
dataIntent.putExtra(message, message);
dataIntent.putExtra(bitmap, bitmap);
dataIntent.putExtra(newId, newId);
setResult(
20
, dataIntent);
finish();
super
.onBackPressed();
}
private
void
getJsonData(String json) {
try
{
JSONObject jsonObject =
new
JSONObject(json);
if
(jsonObject.getBoolean(success)) {
JSONObject jsonObject2 =
new
JSONObject(
jsonObject.getString(yi18));
message = jsonObject2.getString(message);
textView.setText(Html.fromHtml(jsonObject2.getString(message)));
}
}
catch
(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class
DownLoadJson
extends
AsyncTask<string, string=
""
> {
@Override
protected
String doInBackground(String... params) {
return
util.downLoadJson(params[
0
]);
}
@Override
protected
void
onPostExecute(String result) {
if
(result !=
null
) {
getJsonData(result);
}
}
}
class
DownLoadBitmap
extends
AsyncTask<string, bitmap=
""
> {
@Override
protected
Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
return
util.downLoadBitmap(params[
0
]);
}
@Override
protected
void
onPostExecute(Bitmap result) {
if
(result !=
null
) {
bitmap = result;
imageView.setImageBitmap(result);
}
}
}
}</string,></string,>
|
1
2
3
4
5
6
7
8
9
|
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
int
newId = data.getExtras().getInt(newId);
String message = data.getExtras().getString(message);
Bitmap bitmap = data.getParcelableExtra(bitmap);
util.addJsonLruCache(newId, message);
util.addBitmapLruCache(newId, bitmap);
super
.onActivityResult(requestCode, resultCode, data);
}
|