001.
package
com.myapp;
002.
003.
import
java.io.BufferedInputStream;
004.
import
java.io.BufferedOutputStream;
005.
import
java.io.BufferedReader;
006.
import
java.io.ByteArrayOutputStream;
007.
import
java.io.Closeable;
008.
import
java.io.IOException;
009.
import
java.io.InputStream;
010.
import
java.io.InputStreamReader;
011.
import
java.io.OutputStream;
012.
import
java.net.URL;
013.
import
java.util.ArrayList;
014.
import
java.util.HashMap;
015.
016.
import
org.apache.http.HttpEntity;
017.
import
org.apache.http.HttpResponse;
018.
import
org.apache.http.StatusLine;
019.
import
org.apache.http.client.ClientProtocolException;
020.
import
org.apache.http.client.HttpClient;
021.
import
org.apache.http.client.methods.HttpGet;
022.
import
org.apache.http.impl.client.DefaultHttpClient;
023.
import
org.json.JSONArray;
024.
import
org.json.JSONException;
025.
import
org.json.JSONObject;
026.
027.
import
android.app.Activity;
028.
import
android.content.Context;
029.
import
android.graphics.Bitmap;
030.
import
android.graphics.BitmapFactory;
031.
import
android.os.AsyncTask;
032.
import
android.os.Bundle;
033.
import
android.util.Log;
034.
import
android.view.LayoutInflater;
035.
import
android.view.View;
036.
import
android.view.ViewGroup;
037.
import
android.view.Window;
038.
import
android.widget.BaseAdapter;
039.
import
android.widget.Button;
040.
import
android.widget.ImageView;
041.
import
android.widget.ListView;
042.
import
android.widget.TextView;
043.
044.
045.
public
class
MainActivity
extends
Activity {
046.
047.
private
ListView lstView;
048.
private
ImageAdapter imageAdapter;
049.
050.
public
int
currentPage =
1
;
051.
public
int
TotalPage =
0
;
052.
053.
public
Button btnNext;
054.
public
Button btnPre;
055.
056.
ArrayList<HashMap<String, Object>> MyArrList =
new
ArrayList<HashMap<String, Object>>();
057.
058.
@Override
059.
public
void
onCreate(Bundle savedInstanceState) {
060.
super
.onCreate(savedInstanceState);
061.
062.
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
063.
064.
setContentView(R.layout.activity_main);
065.
066.
067.
lstView = (ListView) findViewById(R.id.listView1);
068.
lstView.setClipToPadding(
false
);
069.
imageAdapter =
new
ImageAdapter(getApplicationContext());
070.
lstView.setAdapter(imageAdapter);
071.
072.
073.
074.
btnNext = (Button) findViewById(R.id.btnNext);
075.
076.
btnNext.setOnClickListener(
new
View.OnClickListener() {
077.
public
void
onClick(View v) {
078.
currentPage = currentPage +
1
;
079.
ShowData();
080.
}
081.
});
082.
083.
084.
btnPre = (Button) findViewById(R.id.btnPre);
085.
086.
btnPre.setOnClickListener(
new
View.OnClickListener() {
087.
public
void
onClick(View v) {
088.
currentPage = currentPage -
1
;
089.
ShowData();
090.
}
091.
});
092.
093.
094.
ShowData();
095.
096.
}
097.
098.
public
void
ShowData()
099.
{
100.
btnNext.setEnabled(
false
);
101.
btnPre.setEnabled(
false
);
102.
103.
setProgressBarIndeterminateVisibility(
true
);
104.
new
LoadContentFromServer().execute();
105.
}
106.
107.
108.
class
LoadContentFromServer
extends
AsyncTask<Object, Integer, Object> {
109.
110.
@Override
111.
protected
Object doInBackground(Object... params) {
112.
114.
115.
JSONArray data;
116.
try
{
117.
data =
new
JSONArray(getJSONUrl(url));
118.
119.
MyArrList =
new
ArrayList<HashMap<String, Object>>();
120.
HashMap<String, Object> map;
121.
122.
123.
124.
125.
126.
127.
int
displayPerPage =
7
;
128.
int
TotalRows = data.length();
129.
int
indexRowStart = ((displayPerPage*currentPage)-displayPerPage);
130.
131.
if
(TotalRows<=displayPerPage)
132.
{
133.
TotalPage =
1
;
134.
}
135.
else
if
((TotalRows % displayPerPage)==
0
)
136.
{
137.
TotalPage =(TotalRows/displayPerPage) ;
138.
}
139.
else
140.
{
141.
TotalPage =(TotalRows/displayPerPage)+
1
;
142.
TotalPage = (
int
)TotalPage;
143.
}
144.
int
indexRowEnd = displayPerPage * currentPage;
145.
if
(indexRowEnd > TotalRows)
146.
{
147.
indexRowEnd = TotalRows;
148.
}
149.
150.
for
(
int
i = indexRowStart; i < indexRowEnd; i++){
151.
JSONObject c = data.getJSONObject(i);
152.
map =
new
HashMap<String, Object>();
153.
map.put(
"ImageID"
, (String)c.getString(
"ImageID"
));
154.
map.put(
"ItemID"
, (String)c.getString(
"ItemID"
));
155.
156.
157.
map.put(
"ImagePath"
, (String)c.getString(
"ImagePath"
));
158.
Bitmap newBitmap = loadBitmap(c.getString(
"ImagePath"
));
159.
map.put(
"ImagePathBitmap"
, newBitmap);
160.
161.
MyArrList.add(map);
162.
163.
publishProgress(i);
164.
165.
}
166.
167.
168.
}
catch
(JSONException e) {
169.
170.
e.printStackTrace();
171.
}
172.
173.
return
null
;
174.
}
175.
176.
@Override
177.
public
void
onProgressUpdate(Integer... progress) {
178.
imageAdapter.notifyDataSetChanged();
179.
}
180.
181.
@Override
182.
protected
void
onPostExecute(Object result) {
183.
184.
185.
if
(currentPage >= TotalPage)
186.
{
187.
btnNext.setEnabled(
false
);
188.
}
189.
else
190.
{
191.
btnNext.setEnabled(
true
);
192.
}
193.
194.
195.
if
(currentPage <=
1
)
196.
{
197.
btnPre.setEnabled(
false
);
198.
}
199.
else
200.
{
201.
btnPre.setEnabled(
true
);
202.
}
203.
204.
setProgressBarIndeterminateVisibility(
false
);
205.
}
206.
}
207.
208.
209.
class
ImageAdapter
extends
BaseAdapter {
210.
211.
private
Context mContext;
212.
213.
public
ImageAdapter(Context context) {
214.
mContext = context;
215.
}
216.
217.
public
int
getCount() {
218.
return
MyArrList.size();
219.
}
220.
221.
public
Object getItem(
int
position) {
222.
return
MyArrList.get(position);
223.
}
224.
225.
public
long
getItemId(
int
position) {
226.
return
position;
227.
}
228.
229.
public
View getView(
int
position, View convertView, ViewGroup parent) {
230.
231.
232.
LayoutInflater inflater = (LayoutInflater) mContext
233.
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
234.
235.
236.
if
(convertView ==
null
) {
237.
convertView = inflater.inflate(R.layout.activity_column,
null
);
238.
}
239.
240.
241.
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImagePath);
242.
imageView.getLayoutParams().height =
60
;
243.
imageView.getLayoutParams().width =
60
;
244.
imageView.setPadding(
5
,
5
,
5
,
5
);
245.
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
246.
try
247.
{
248.
imageView.setImageBitmap((Bitmap)MyArrList.get(position).get(
"ImagePathBitmap"
));
249.
}
catch
(Exception e) {
250.
251.
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
252.
}
253.
254.
255.
TextView txtImgID = (TextView) convertView.findViewById(R.id.ColImageID);
256.
txtImgID.setPadding(
10
,
0
,
0
,
0
);
257.
txtImgID.setText(
"ID : "
+ MyArrList.get(position).get(
"ImageID"
).toString());
258.
259.
260.
TextView txtItemID = (TextView) convertView.findViewById(R.id.ColItemID);
261.
txtItemID.setPadding(
50
,
0
,
0
,
0
);
262.
txtItemID.setText(
"Item : "
+ MyArrList.get(position).get(
"ItemID"
).toString());
263.
264.
return
convertView;
265.
266.
}
267.
268.
}
269.
270.
271.
public
String getJSONUrl(String url) {
272.
StringBuilder str =
new
StringBuilder();
273.
HttpClient client =
new
DefaultHttpClient();
274.
HttpGet httpGet =
new
HttpGet(url);
275.
try
{
276.
HttpResponse response = client.execute(httpGet);
277.
StatusLine statusLine = response.getStatusLine();
278.
int
statusCode = statusLine.getStatusCode();
279.
if
(statusCode ==
200
) {
280.
HttpEntity entity = response.getEntity();
281.
InputStream content = entity.getContent();
282.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(content));
283.
String line;
284.
while
((line = reader.readLine()) !=
null
) {
285.
str.append(line);
286.
}
287.
}
else
{
288.
Log.e(
"Log"
,
"Failed to download file.."
);
289.
}
290.
}
catch
(ClientProtocolException e) {
291.
e.printStackTrace();
292.
}
catch
(IOException e) {
293.
e.printStackTrace();
294.
}
295.
return
str.toString();
296.
}
297.
298.
299.
private
static
final
String TAG =
"Image"
;
300.
private
static
final
int
IO_BUFFER_SIZE =
4
*
1024
;
301.
public
static
Bitmap loadBitmap(String url) {
302.
Bitmap bitmap =
null
;
303.
InputStream in =
null
;
304.
BufferedOutputStream out =
null
;
305.
306.
try
{
307.
in =
new
BufferedInputStream(
new
URL(url).openStream(), IO_BUFFER_SIZE);
308.
309.
final
ByteArrayOutputStream dataStream =
new
ByteArrayOutputStream();
310.
out =
new
BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
311.
copy(in, out);
312.
out.flush();
313.
314.
final
byte
[] data = dataStream.toByteArray();
315.
BitmapFactory.Options options =
new
BitmapFactory.Options();
316.
317.
318.
bitmap = BitmapFactory.decodeByteArray(data,
0
, data.length,options);
319.
}
catch
(IOException e) {
320.
Log.e(TAG,
"Could not load Bitmap from: "
+ url);
321.
}
finally
{
322.
closeStream(in);
323.
closeStream(out);
324.
}
325.
326.
return
bitmap;
327.
}
328.
329.
private
static
void
closeStream(Closeable stream) {
330.
if
(stream !=
null
) {
331.
try
{
332.
stream.close();
333.
}
catch
(IOException e) {
334.
android.util.Log.e(TAG,
"Could not close stream"
, e);
335.
}
336.
}
337.
}
338.
339.
private
static
void
copy(InputStream in, OutputStream out)
throws
IOException {
340.
byte
[] b =
new
byte
[IO_BUFFER_SIZE];
341.
int
read;
342.
while
((read = in.read(b)) != -
1
) {
343.
out.write(b,
0
, read);
344.
}
345.
}
346.
347.
348.
}