package com.example.komunitas;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Galeri extends AppCompatActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONArray myJSON = null;
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
int jd=0;
String ip="";
String[]arId_galeri;
String[]arJudul;
String[]arTgl;
String[]arJam;
String[]arKeterangan;
String[]arKode_anggota;
String[]arGambar;
Bitmap []arBitmap;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ip=jParser.getIP();
new load().execute();
}
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<>();
//TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids); for (int i = 0; i < jd; i++) {//jd/imgs.length() //Bitmap bitmap = BitmapFactory.decodeResource(getResources(),arBitmap[i]);// imgs.getResourceId(i, -1) imageItems.add(new ImageItem(arBitmap[i], arJudul[i]));
}
return imageItems;
}
class load extends AsyncTask<String, String, String> {
@Override protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Galeri.this);
pDialog.setMessage("Load data. Silahkan Tunggu...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(ip+"galeri/galeri_show.php", "GET", params);
// Log.d("show: ", json.toString()); try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
myJSON = json.getJSONArray(TAG_record);
jd=myJSON.length();
arId_galeri=new String[jd];
arJudul=new String[jd];
arTgl=new String[jd];
arJam=new String[jd];
arKeterangan=new String[jd];
arKode_anggota=new String[jd];
arGambar=new String[jd];
arBitmap=new Bitmap[jd];
for (int i = 0; i < myJSON.length(); i++) {
JSONObject c = myJSON.getJSONObject(i);
arId_galeri[i]= c.getString("id_galeri");
arJudul[i]= c.getString("judul");
arTgl[i]= c.getString("tgl");
arJam[i]= c.getString("jam");
arKeterangan[i]= c.getString("keterangan");
arKode_anggota[i]= c.getString("nama_anggota");
arGambar[i]= c.getString("gambar");
String gb=ip+"ypathfile/"+ arGambar[i];
Log.v("URL",gb);
arBitmap[i]=getBitmapFromURL(gb);
}
}
}
catch (JSONException e) {e.printStackTrace();}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(Galeri.this, R.layout.grid_item_layout, getData());
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageItem item = (ImageItem) parent.getItemAtPosition(position);
Intent intent = new Intent(Galeri.this, DetailGaleri.class);
intent.putExtra("title", arJudul[position]);//item.getTitle() intent.putExtra("title2", arTgl[position]);
intent.putExtra("title3", arKeterangan[position]);
intent.putExtra("title4", arKode_anggota[position]);
intent.putExtra("waktu", arTgl[position]+" "+arJam[position]+" Wib");
intent.putExtra("gambar", arGambar[position]);
//intent.putExtra("image", item.getImage()); startActivity(intent);
}
});
}
});}
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
// Log exception return null;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 0, "Upload foto");//.setIcon(R.drawable.add); return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Intent i = new Intent(getApplicationContext(), UploadToServer.class);
startActivityForResult(i, 100);
finish();
return true;
}
return false;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++
package com.kriptograph.mygaleri;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private int layoutResourceId;
private ArrayList<ImageItem> data = new ArrayList<ImageItem>();
public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0f0f0">
<GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:columnWidth="100dp" android:drawSelectorOnTop="true" android:gravity="center" android:numColumns="2" android:stretchMode="columnWidth" android:verticalSpacing="5dp" android:focusable="true" android:clickable="true"/>
</RelativeLayout>
grid_item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@drawable/grid_color_selector" android:orientation="vertical" android:padding="5dp">
<ImageView android:id="@+id/image" android:layout_width="160dp" android:layout_height="150dp" />
<TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:textSize="12sp" />
</LinearLayout>
ImageItem.java
package com.kriptograph.mygaleri;
import android.graphics.Bitmap;
public class ImageItem {
private Bitmap image;
private String title;
public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Download
Tidak ada komentar:
Posting Komentar