> list) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
mInflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
this.list = list;
mViewResourceId = textViewResourceId;
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
//resize image base on screen density
float density = convertView.getResources().getDisplayMetrics().density;
int hwdpi = (int) (density*36);
ImageView iv = (ImageView)convertView.findViewById(R.id.photo);
Bitmap scaled = Bitmap.createScaledBitmap((Bitmap) list.get(position).get("photo"), hwdpi, hwdpi, true);
iv.setImageBitmap((Bitmap) scaled);
TextView baris1 = (TextView)convertView.findViewById(R.id.baris1);
baris1.setText((CharSequence) list.get(position).get("nim"));
TextView baris2 = (TextView)convertView.findViewById(R.id.baris2);
baris2.setText((CharSequence) list.get(position).get("nama"));
return convertView;
}
}
DataSiswaFotoActivity.java
package com.tkj;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class DataSiswaFotoActivity extends ListActivity {
private SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
openDatabase();
refreshView();
}
public void buttonTambahOnClick(View v) {
Intent intent = new Intent();
intent.setClass(this, TambahData.class);
startActivityForResult(intent, 1);
}
public void refreshView() {
ArrayList> list = new ArrayList>();
Cursor cursor = db.query(false, "Siswa", new String[] { "nim", "nama",
"photo" }, null, null, null, null, null, null);
while (cursor.moveToNext()) {
HashMap item = new HashMap();
byte[] byteArray=cursor.getBlob(cursor.getColumnIndex("photo"));
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
item.put("photo", bitmap);
item.put("nim", cursor.getString(0));
item.put("nama", cursor.getString(1));
list.add(item);
}
ImageandTextAdapter notes = new ImageandTextAdapter(this, R.layout.main_list, list);
setListAdapter(notes);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
ContentValues initialValues = new ContentValues();
initialValues.put("nim", data.getStringExtra("nim"));
initialValues.put("nama", data.getStringExtra("nama"));
byte[] byteArray = data.getByteArrayExtra("photo");
initialValues.put("photo", byteArray);
long hasil = db.insert("Siswa", null, initialValues);
if (hasil < 0) {
tampilToast("Data Gagal Disimpan!");
} else {
refreshView();
}
}
}
public void openDatabase() {
MyDBOpenHelper myOpenHelper = new MyDBOpenHelper(
this.getApplicationContext());
db = myOpenHelper.getWritableDatabase();
}
public void closeDatabase() {
db.close();
}
private void tampilToast(String pesan) {
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), pesan, duration);
toast.show();
}
}
TambahData.java
package com.tkj;
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class TambahData extends Activity {
private static final int CAMERA_PIC_REQUEST = 1337;
Bitmap photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tambahdata);
}
public void buttonCaptureOnClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK
&& requestCode == CAMERA_PIC_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imagePhoto);
image.setImageBitmap(photo);
}
}
public void buttonSimpanOnClick(View v) {
TextView nim = (TextView) findViewById(R.id.editNim);
TextView nama = (TextView) findViewById(R.id.editNama);
Intent intent = new Intent();
intent.putExtra("nim", nim.getText().toString());
intent.putExtra("nama", nama.getText().toString());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("photo", byteArray);
this.setResult(RESULT_OK, intent);
finish();
}
}