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.
import
java.util.List;
016.
017.
import
org.apache.http.HttpEntity;
018.
import
org.apache.http.HttpResponse;
019.
import
org.apache.http.NameValuePair;
020.
import
org.apache.http.StatusLine;
021.
import
org.apache.http.client.ClientProtocolException;
022.
import
org.apache.http.client.HttpClient;
023.
import
org.apache.http.client.entity.UrlEncodedFormEntity;
024.
import
org.apache.http.client.methods.HttpGet;
025.
import
org.apache.http.client.methods.HttpPost;
026.
import
org.apache.http.impl.client.DefaultHttpClient;
027.
import
org.apache.http.message.BasicNameValuePair;
028.
import
org.json.JSONArray;
029.
import
org.json.JSONException;
030.
import
org.json.JSONObject;
031.
032.
import
android.os.AsyncTask;
033.
import
android.os.Bundle;
034.
import
android.os.StrictMode;
035.
import
android.annotation.SuppressLint;
036.
import
android.app.Activity;
037.
import
android.app.AlertDialog;
038.
import
android.app.Dialog;
039.
import
android.app.ProgressDialog;
040.
import
android.content.Context;
041.
import
android.content.DialogInterface;
042.
import
android.graphics.Bitmap;
043.
import
android.graphics.BitmapFactory;
044.
import
android.util.Log;
045.
import
android.view.LayoutInflater;
046.
import
android.view.View;
047.
import
android.view.Menu;
048.
import
android.view.ViewGroup;
049.
import
android.widget.BaseAdapter;
050.
import
android.widget.ImageView;
051.
import
android.widget.ListView;
052.
import
android.widget.RatingBar;
053.
import
android.widget.TextView;
054.
import
android.widget.Toast;
055.
056.
public
class
MainActivity
extends
Activity {
057.
058.
public
static
final
int
DIALOG_DOWNLOAD_JSON_PROGRESS =
0
;
059.
private
ProgressDialog mProgressDialog;
060.
061.
ArrayList<HashMap<String, Object>> MyArrList;
062.
063.
ListView lstView1;
064.
065.
066.
@SuppressLint
(
"NewApi"
)
067.
@Override
068.
public
void
onCreate(Bundle savedInstanceState) {
069.
super
.onCreate(savedInstanceState);
070.
setContentView(R.layout.activity_main);
071.
072.
073.
if
(android.os.Build.VERSION.SDK_INT >
9
) {
074.
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder().permitAll().build();
075.
StrictMode.setThreadPolicy(policy);
076.
}
077.
078.
079.
new
DownloadJSONFileAsync().execute();
080.
081.
}
082.
083.
084.
@Override
085.
protected
Dialog onCreateDialog(
int
id) {
086.
switch
(id) {
087.
case
DIALOG_DOWNLOAD_JSON_PROGRESS:
088.
mProgressDialog =
new
ProgressDialog(
this
);
089.
mProgressDialog.setMessage(
"Downloading....."
);
090.
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
091.
mProgressDialog.setCancelable(
true
);
092.
mProgressDialog.show();
093.
return
mProgressDialog;
094.
default
:
095.
return
null
;
096.
}
097.
}
098.
099.
100.
public
void
ShowAllContent()
101.
{
102.
103.
lstView1 = (ListView)findViewById(R.id.listView1);
104.
lstView1.setAdapter(
new
ImageAdapter(MainActivity.
this
,MyArrList));
105.
106.
}
107.
108.
109.
public
class
ImageAdapter
extends
BaseAdapter
110.
{
111.
private
Context context;
112.
private
ArrayList<HashMap<String, Object>> MyArr =
new
ArrayList<HashMap<String, Object>>();
113.
114.
public
ImageAdapter(Context c, ArrayList<HashMap<String, Object>> myArrList)
115.
{
116.
117.
context = c;
118.
MyArr = myArrList;
119.
}
120.
121.
public
int
getCount() {
122.
123.
return
MyArr.size();
124.
}
125.
126.
public
Object getItem(
int
position) {
127.
128.
return
position;
129.
}
130.
131.
public
long
getItemId(
int
position) {
132.
133.
return
position;
134.
}
135.
public
View getView(
final
int
position, View convertView, ViewGroup parent) {
136.
137.
138.
LayoutInflater inflater = (LayoutInflater) context
139.
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
140.
141.
142.
if
(convertView ==
null
) {
143.
convertView = inflater.inflate(R.layout.activity_column,
null
);
144.
}
145.
146.
147.
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
148.
imageView.getLayoutParams().height =
80
;
149.
imageView.getLayoutParams().width =
80
;
150.
imageView.setPadding(
10
,
10
,
10
,
10
);
151.
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
152.
try
153.
{
154.
imageView.setImageBitmap((Bitmap)MyArr.get(position).get(
"ImageThumBitmap"
));
155.
}
catch
(Exception e) {
156.
157.
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
158.
}
159.
160.
161.
imageView.setOnClickListener(
new
View.OnClickListener() {
162.
public
void
onClick(View v) {
163.
String strImageID = MyArr.get(position).get(
"ImageID"
).toString();
164.
String strImageName = MyArr.get(position).get(
"ImageName"
).toString();
165.
String strCurrentRating = MyArr.get(position).get(
"Rating"
).toString();
166.
167.
ShowDialogVote(position, strImageID,
168.
strImageName, strCurrentRating,
169.
MyArr.get(position).get(
"ImagePathFull"
).toString());
170.
171.
}
172.
});
173.
174.
175.
176.
TextView txtImgID = (TextView) convertView.findViewById(R.id.ColImgID);
177.
txtImgID.setPadding(
5
,
0
,
0
,
0
);
178.
txtImgID.setText(
"ID : "
+ MyArr.get(position).get(
"ImageID"
).toString());
179.
180.
181.
TextView txtPicName = (TextView) convertView.findViewById(R.id.ColImgName);
182.
txtPicName.setPadding(
5
,
0
,
0
,
0
);
183.
txtPicName.setText(
"Name : "
+ MyArr.get(position).get(
"ImageName"
).toString());
184.
185.
186.
RatingBar Rating = (RatingBar) convertView.findViewById(R.id.ColratingBar);
187.
Rating.setPadding(
10
,
0
,
0
,
0
);
188.
Rating.setEnabled(
false
);
189.
Rating.setMax(
5
);
190.
Rating.setRating(Float.valueOf(MyArr.get(position).get(
"Rating"
).toString()));
191.
192.
return
convertView;
193.
194.
}
195.
196.
}
197.
198.
199.
public
void
ShowDialogVote(
final
int
position,
final
String strImageID,
200.
String strImageName,
final
String strCurrentRating,String FullImagePath)
201.
{
202.
203.
final
AlertDialog.Builder popDialog =
new
AlertDialog.Builder(
this
);
204.
final
AlertDialog.Builder adb =
new
AlertDialog.Builder(
this
);
205.
final
LayoutInflater inflater = (LayoutInflater)
this
.getSystemService(LAYOUT_INFLATER_SERVICE);
206.
207.
final
View Viewlayout = inflater.inflate(R.layout.activity_dialog,
208.
(ViewGroup) findViewById(R.id.layout_dialog));
209.
210.
final
ImageView image = (ImageView)Viewlayout.findViewById(R.id.imageView);
211.
image.setImageBitmap((Bitmap)loadBitmap(FullImagePath));
212.
213.
final
RatingBar rating = (RatingBar)Viewlayout.findViewById(R.id.ratingBar);
214.
rating.setMax(
5
);
215.
rating.setNumStars(
5
);
216.
217.
popDialog.setIcon(android.R.drawable.btn_star_big_on);
218.
popDialog.setTitle(
"Vote!! ("
+ strImageName +
") "
);
219.
popDialog.setView(Viewlayout);
220.
221.
222.
popDialog.setPositiveButton(
"OK"
,
223.
new
DialogInterface.OnClickListener() {
224.
public
void
onClick(DialogInterface dialog,
int
which) {
225.
226.
228.
229.
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
230.
params.add(
new
BasicNameValuePair(
"ImageID"
, strImageID));
231.
params.add(
new
BasicNameValuePair(
"ratingPoint"
, String.valueOf(rating.getRating())));
232.
233.
String resultServer = getHttpPost(url,params);
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
String strStatusID =
"0"
;
246.
String strError =
"Unknow Status!"
;
247.
String strRatingPoint = strCurrentRating;
248.
249.
JSONObject c;
250.
try
{
251.
c =
new
JSONObject(resultServer);
252.
strStatusID = c.getString(
"StatusID"
);
253.
strError = c.getString(
"Error"
);
254.
strRatingPoint = c.getString(
"Rating"
);
255.
}
catch
(JSONException e) {
256.
257.
e.printStackTrace();
258.
}
259.
260.
261.
if
(strStatusID.equals(
"0"
))
262.
{
263.
adb.setMessage(strError);
264.
adb.show();
265.
}
266.
else
267.
{
268.
Log.d(
"strRatingPoint"
,strRatingPoint);
269.
UpdateNewRatingPoint(position,strRatingPoint);
270.
271.
Toast.makeText(MainActivity.
this
,
"Vote Finished (Point : "
+ rating.getRating() +
")"
, Toast.LENGTH_LONG).show();
272.
}
273.
274.
dialog.dismiss();
275.
276.
277.
}
278.
279.
})
280.
281.
282.
.setNegativeButton(
"Cancel"
,
283.
new
DialogInterface.OnClickListener() {
284.
public
void
onClick(DialogInterface dialog,
int
id) {
285.
dialog.cancel();
286.
}
287.
});
288.
289.
popDialog.create();
290.
popDialog.show();
291.
292.
}
293.
294.
295.
296.
private
void
UpdateNewRatingPoint(
int
position,String newRatingPoint){
297.
298.
View v = lstView1.getChildAt(position - lstView1.getFirstVisiblePosition());
299.
300.
301.
RatingBar rating = (RatingBar)v.findViewById(R.id.ColratingBar);
302.
rating.setEnabled(
true
);
303.
rating.setRating(Float.valueOf(newRatingPoint));
304.
rating.setEnabled(
false
);
305.
}
306.
307.
308.
309.
310.
public
class
DownloadJSONFileAsync
extends
AsyncTask<String, Void, Void> {
311.
312.
protected
void
onPreExecute() {
313.
super
.onPreExecute();
314.
showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
315.
}
316.
317.
@Override
318.
protected
Void doInBackground(String... params) {
319.
320.
322.
323.
JSONArray data;
324.
try
{
325.
data =
new
JSONArray(getJSONUrl(url));
326.
327.
MyArrList =
new
ArrayList<HashMap<String, Object>>();
328.
HashMap<String, Object> map;
329.
330.
for
(
int
i =
0
; i < data.length(); i++){
331.
JSONObject c = data.getJSONObject(i);
332.
map =
new
HashMap<String, Object>();
333.
map.put(
"ImageID"
, (String)c.getString(
"ImageID"
));
334.
map.put(
"ImageName"
, (String)c.getString(
"ImageName"
));
335.
336.
337.
map.put(
"ImagePathThum"
, (String)c.getString(
"ImagePath_Thumbnail"
));
338.
map.put(
"ImageThumBitmap"
, (Bitmap)loadBitmap(c.getString(
"ImagePath_Thumbnail"
)));
339.
340.
341.
map.put(
"ImagePathFull"
, (String)c.getString(
"ImagePath_FullPhoto"
));
342.
343.
map.put(
"Rating"
, (String)c.getString(
"Rating"
));
344.
345.
MyArrList.add(map);
346.
}
347.
348.
349.
}
catch
(JSONException e) {
350.
351.
e.printStackTrace();
352.
}
353.
354.
return
null
;
355.
}
356.
357.
protected
void
onPostExecute(Void unused) {
358.
ShowAllContent();
359.
dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
360.
removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
361.
}
362.
363.
}
364.
365.
366.
public
String getHttpPost(String url,List<NameValuePair> params) {
367.
StringBuilder str =
new
StringBuilder();
368.
HttpClient client =
new
DefaultHttpClient();
369.
HttpPost httpPost =
new
HttpPost(url);
370.
371.
try
{
372.
httpPost.setEntity(
new
UrlEncodedFormEntity(params));
373.
HttpResponse response = client.execute(httpPost);
374.
StatusLine statusLine = response.getStatusLine();
375.
int
statusCode = statusLine.getStatusCode();
376.
if
(statusCode ==
200
) {
377.
HttpEntity entity = response.getEntity();
378.
InputStream content = entity.getContent();
379.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(content));
380.
String line;
381.
while
((line = reader.readLine()) !=
null
) {
382.
str.append(line);
383.
}
384.
}
else
{
385.
Log.e(
"Log"
,
"Failed to download result.."
);
386.
}
387.
}
catch
(ClientProtocolException e) {
388.
e.printStackTrace();
389.
}
catch
(IOException e) {
390.
e.printStackTrace();
391.
}
392.
return
str.toString();
393.
}
394.
395.
396.
397.
public
String getJSONUrl(String url) {
398.
StringBuilder str =
new
StringBuilder();
399.
HttpClient client =
new
DefaultHttpClient();
400.
HttpGet httpGet =
new
HttpGet(url);
401.
try
{
402.
HttpResponse response = client.execute(httpGet);
403.
StatusLine statusLine = response.getStatusLine();
404.
int
statusCode = statusLine.getStatusCode();
405.
if
(statusCode ==
200
) {
406.
HttpEntity entity = response.getEntity();
407.
InputStream content = entity.getContent();
408.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(content));
409.
String line;
410.
while
((line = reader.readLine()) !=
null
) {
411.
str.append(line);
412.
}
413.
}
else
{
414.
Log.e(
"Log"
,
"Failed to download file.."
);
415.
}
416.
}
catch
(ClientProtocolException e) {
417.
e.printStackTrace();
418.
}
catch
(IOException e) {
419.
e.printStackTrace();
420.
}
421.
return
str.toString();
422.
}
423.
424.
425.
private
static
final
String TAG =
"Image"
;
426.
private
static
final
int
IO_BUFFER_SIZE =
4
*
1024
;
427.
public
static
Bitmap loadBitmap(String url) {
428.
Bitmap bitmap =
null
;
429.
InputStream in =
null
;
430.
BufferedOutputStream out =
null
;
431.
432.
try
{
433.
in =
new
BufferedInputStream(
new
URL(url).openStream(), IO_BUFFER_SIZE);
434.
435.
final
ByteArrayOutputStream dataStream =
new
ByteArrayOutputStream();
436.
out =
new
BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
437.
copy(in, out);
438.
out.flush();
439.
440.
final
byte
[] data = dataStream.toByteArray();
441.
BitmapFactory.Options options =
new
BitmapFactory.Options();
442.
443.
444.
bitmap = BitmapFactory.decodeByteArray(data,
0
, data.length,options);
445.
}
catch
(IOException e) {
446.
Log.e(TAG,
"Could not load Bitmap from: "
+ url);
447.
}
finally
{
448.
closeStream(in);
449.
closeStream(out);
450.
}
451.
452.
return
bitmap;
453.
}
454.
455.
private
static
void
closeStream(Closeable stream) {
456.
if
(stream !=
null
) {
457.
try
{
458.
stream.close();
459.
}
catch
(IOException e) {
460.
android.util.Log.e(TAG,
"Could not close stream"
, e);
461.
}
462.
}
463.
}
464.
465.
private
static
void
copy(InputStream in, OutputStream out)
throws
IOException {
466.
byte
[] b =
new
byte
[IO_BUFFER_SIZE];
467.
int
read;
468.
while
((read = in.read(b)) != -
1
) {
469.
out.write(b,
0
, read);
470.
}
471.
}
472.
473.
474.
@Override
475.
public
boolean
onCreateOptionsMenu(Menu menu) {
476.
getMenuInflater().inflate(R.menu.activity_main, menu);
477.
return
true
;
478.
}
479.
480.
}