Script Desain Layout:
1. daftarkerusakan.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity2"
android:background="@drawable/mbackground">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_marginTop="37dp"
android:layout_height="38dp"
android:text="Back"
android:id="@+id/kembali" />
<ListView
android:id="@+id/pro_lsit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/kembali"
android:background="#ffb8ffa9"
android:layout_alignParentStart="true"
android:divider="#FF00FF00"
android:dividerHeight="3px"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="42dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DATA KERUSAKAN CPU"
android:id="@+id/txt_data_kerusakan"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff"
android:textStyle="bold"
android:gravity="center"
android:background="#ff000000" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView4"
android:layout_above="@+id/btntambahdata_Period"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tambah Data"
android:id="@+id/btntambahdata_Period"
android:layout_alignParentBottom="true"
android:onClick="tambahRow"
android:layout_alignRight="@+id/pro_lsit"
android:layout_alignEnd="@+id/pro_lsit" />
</RelativeLayout>
tampilannya, lihat pada gambar dibawah ini:
2. list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="2dp"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_delete_period"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:text="Hapus"
android:layout_alignParentRight="true"
android:onClick="deleteRow"
android:textSize="11dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="32dp"
android:text="EDIT"
android:id="@+id/bt_edit_period"
android:layout_below="@+id/bt_delete_period"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="editRow"
android:textSize="11dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kode Kerusakan"
android:id="@+id/kode"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textColor="#ff030303"
android:textSize="12dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kategori Hardware"
android:id="@+id/namahardware"
android:textColor="#ff080808"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_above="@+id/bt_edit_period"
android:layout_alignParentStart="true"
android:textSize="12dp" />
<TextView
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:text="Ciri Kerusakan"
android:id="@+id/cirikerusakan"
android:textColor="#ff050505"
android:textSize="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignBaseline="@+id/bt_edit_period"
android:layout_alignBottom="@+id/bt_edit_period"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:text="Penyebab Kerusakan"
android:id="@+id/penyebabkerusakan"
android:textColor="#ff040404"
android:textSize="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/cirikerusakan"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="2dp"
android:layout_height="wrap_content"
android:text="Solusi Kerusakan"
android:id="@+id/solusikerusakan"
android:textColor="#ff000000"
android:textSize="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/penyebabkerusakan"
android:layout_alignParentStart="true"
android:layout_marginBottom="8dp"/>
</RelativeLayout>
tampilannya, lihat pada gambar dibawah ini:
2. CustomAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<String>{
int groupid;
ArrayList<String> records;
Context context;
public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
String[] row_items=records.get(position).split("__");
TextView t4id= (TextView) itemView.findViewById(R.id.kode);
t4id.setText("Kode Kerusakan : "+row_items[0]);
TextView nama= (TextView) itemView.findViewById(R.id.namahardware);
nama.setText("Nama Hardware : "+row_items[1]);
TextView ciri= (TextView) itemView.findViewById(R.id.cirikerusakan);
ciri.setText("Ciri Kerusakan : "+row_items[2]);
TextView penyebab= (TextView) itemView.findViewById(R.id.penyebabkerusakan);
penyebab.setText("Penyebab Kerusakan : "+row_items[3]);
TextView solusi= (TextView) itemView.findViewById(R.id.solusikerusakan);
solusi.setText("Solusi Kerusakan : "+row_items[4]);
Button bt_del_period=(Button)itemView.findViewById(R.id.bt_delete_period);
bt_del_period.setTag(row_items[0]);
Button bt_edit_period=(Button)itemView.findViewById(R.id.bt_edit_period);
bt_edit_period.setTag(row_items[0]);
return itemView;
}
}
1. daftarkerusakan.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity2"
android:background="@drawable/mbackground">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_marginTop="37dp"
android:layout_height="38dp"
android:text="Back"
android:id="@+id/kembali" />
<ListView
android:id="@+id/pro_lsit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/kembali"
android:background="#ffb8ffa9"
android:layout_alignParentStart="true"
android:divider="#FF00FF00"
android:dividerHeight="3px"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="42dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DATA KERUSAKAN CPU"
android:id="@+id/txt_data_kerusakan"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff"
android:textStyle="bold"
android:gravity="center"
android:background="#ff000000" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView4"
android:layout_above="@+id/btntambahdata_Period"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tambah Data"
android:id="@+id/btntambahdata_Period"
android:layout_alignParentBottom="true"
android:onClick="tambahRow"
android:layout_alignRight="@+id/pro_lsit"
android:layout_alignEnd="@+id/pro_lsit" />
</RelativeLayout>
tampilannya, lihat pada gambar dibawah ini:
2. list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="2dp"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_delete_period"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:text="Hapus"
android:layout_alignParentRight="true"
android:onClick="deleteRow"
android:textSize="11dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="32dp"
android:text="EDIT"
android:id="@+id/bt_edit_period"
android:layout_below="@+id/bt_delete_period"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="editRow"
android:textSize="11dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kode Kerusakan"
android:id="@+id/kode"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textColor="#ff030303"
android:textSize="12dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kategori Hardware"
android:id="@+id/namahardware"
android:textColor="#ff080808"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_above="@+id/bt_edit_period"
android:layout_alignParentStart="true"
android:textSize="12dp" />
<TextView
android:layout_width= "wrap_content"
android:layout_height="wrap_content"
android:text="Ciri Kerusakan"
android:id="@+id/cirikerusakan"
android:textColor="#ff050505"
android:textSize="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignBaseline="@+id/bt_edit_period"
android:layout_alignBottom="@+id/bt_edit_period"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:text="Penyebab Kerusakan"
android:id="@+id/penyebabkerusakan"
android:textColor="#ff040404"
android:textSize="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/cirikerusakan"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="2dp"
android:layout_height="wrap_content"
android:text="Solusi Kerusakan"
android:id="@+id/solusikerusakan"
android:textColor="#ff000000"
android:textSize="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@+id/penyebabkerusakan"
android:layout_alignParentStart="true"
android:layout_marginBottom="8dp"/>
</RelativeLayout>
tampilannya, lihat pada gambar dibawah ini:
3. tambahdatacirikerusakan.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/namahardware"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Kategori Hardware"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/cirikerusakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/namahardware"
android:hint="Ciri Kerusakan"
android:singleLine="true" />
<EditText
android:id="@+id/penyebabkerusakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cirikerusakan"
android:hint="Penyebab Kerusakan"
android:singleLine="true" />
<EditText
android:id="@+id/solusikerusakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/penyebabkerusakan"
android:hint="Solusi Kerusakan"
android:singleLine="true" />
</RelativeLayout>
tampilannya, lihat pada gambar dibawah ini:
4. editdatacirikerusakan.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/namahardware"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Kategori Hardware"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/cirikerusakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/namahardware"
android:hint="Ciri Kerusakan"
android:singleLine="true" />
<EditText
android:id="@+id/penyebabkerusakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/cirikerusakan"
android:hint="Penyebab Kerusakan"
android:singleLine="true" />
<EditText
android:id="@+id/solusikerusakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/penyebabkerusakan"
android:hint="Solusi Kerusakan"
android:singleLine="true" />
</RelativeLayout>
tampilannya, lihat pada gambar dibawah ini:
Script Java:
1. MainActivity2.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity2 extends Activity {
ListView listview;
CustomAdapter adapter;
ArrayList<String> records;
SQLiteDB dbhelper;
//Button btntambahdata_kerusakan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daftarkerusakan);
//final Shader shader = new LinearGradient(0, 0, 0, 20,
// new int[]{Color.GREEN,Color.BLUE},
// new float[]{0, 1}, Shader.TileMode.CLAMP
//);
listview=(ListView)findViewById(R.id.pro_lsit);
records=new ArrayList<String>();
adapter=new CustomAdapter(this,R.layout.list_item,R.id.kode,records);
listview.setAdapter(adapter);
dbhelper=new SQLiteDB(this);
listview.setDivider(new ColorDrawable(0xFF00FF00)); //0xAARRGGBB
listview.setDividerHeight(3);
// final TextView txt_data_kerusakan = (TextView) findViewById(R.id.txt_data_kerusakan);
// txt_data_kerusakan.getPaint().setShader(shader);
Button go = (Button)findViewById(R.id.kembali);
go.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
public void onStart(){
super.onStart();
readSampleData();
countRecords_period();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void readSampleData(){
//MEMBUKA DATABASE
SQLiteDatabase database=dbhelper.getReadableDatabase();
String sql="SELECT * FROM "+ SQLiteDB.MYDATABASE_TABLE;
//MEMBUAT KURSOR UNTUK MEMBUKA DATABASE
Cursor c=database.rawQuery(sql,null);
String isinamahardware, isicirikerusakan, isipenyebabkerusakan, isisolusikerusakan;
int id;
if(c.getCount()>0)
while(c.moveToNext()){
id=c.getInt(c.getColumnIndex(SQLiteDB.KEY_ID));
isinamahardware=c.getString(c.getColumnIndex(SQLiteDB.KEY_KATEGORIHARDWARE));
isicirikerusakan=c.getString(c.getColumnIndex(SQLiteDB.KEY_CIRIKERUSAKAN));
isipenyebabkerusakan=c.getString(c.getColumnIndex(SQLiteDB.KEY_PENYEBABKERUSAKAN));
isisolusikerusakan=c.getString(c.getColumnIndex(SQLiteDB.KEY_SOLUSIKERUSAKAN));
String item=id+"__"+isinamahardware+"__"+isicirikerusakan+"__"+isipenyebabkerusakan+"__"+isisolusikerusakan;
records.add(item);
}
//notify listview of dataset changed
adapter.notifyDataSetChanged();
}
// HAPUS DATA DARI TABEL
public void deleteRow(View view){
Button bt=(Button)view;
final String del_id1=bt.getTag().toString();
//MENAMPILKAN PERINGATAN MENGHAPUS DATA
AlertDialog.Builder alertDialogHapus = new AlertDialog.Builder(MainActivity2.this);
alertDialogHapus.setTitle("Hapus Data");
alertDialogHapus.setMessage("Anda yakin ingin menghapus data ini?");
//KALAU YAKIN HAPUS
alertDialogHapus.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SQLiteDatabase database = dbhelper.getWritableDatabase();
database.delete(SQLiteDB.MYDATABASE_TABLE, SQLiteDB.KEY_ID + "=?", new String[]{del_id1});
for (int i = 0; i < records.size(); i++) {
if (records.get(i).startsWith(del_id1))
records.remove(i);
}
adapter.notifyDataSetChanged();
countRecords_period();
Toast.makeText(MainActivity2.this, "Data berhasil dihapus", Toast.LENGTH_SHORT).show();
}
});
//KALAU TIDAK YAKIN JANGAN DIHAPUS
alertDialogHapus.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialogHapus.show();
}
// TAMPILKAN JUMLAH DATA YANG ADA DALAM TABEL
public void countRecords_period() {
SQLiteDatabase database=dbhelper.getWritableDatabase();
String sql = "SELECT * FROM mslh_cpu";
int recordCount = database.rawQuery(sql, null).getCount();
database.close();
TextView textViewRecordCount = (TextView) findViewById(R.id.txt_data_kerusakan);
textViewRecordCount.setText("DATA KERUSAKAN (" + recordCount + " data)");
}
// EDIT DATA DARI TABEL
public void editRow(View view){
Button bt_edit = (Button)view;
SQLiteDatabase database = dbhelper.getWritableDatabase();
final String del_id = bt_edit.getTag().toString();
String sql_ambil_1_data = "SELECT * FROM mslh_cpu WHERE _id = " + del_id;
final Cursor cursor = database.rawQuery(sql_ambil_1_data, null);
final Context context = view.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.editdatacirikerusakan, null, false);
final EditText kode = (EditText) formElementsView.findViewById(R.id.kode);
final EditText nama = (EditText) formElementsView.findViewById(R.id.namahardware);
final EditText ciri = (EditText) formElementsView.findViewById(R.id.cirikerusakan);
final EditText penyebab = (EditText) formElementsView.findViewById(R.id.penyebabkerusakan);
final EditText solusi = (EditText) formElementsView.findViewById(R.id.solusikerusakan);
if (cursor.moveToFirst()) {
int id_per = Integer.parseInt(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_ID)));
String nama_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_KATEGORIHARDWARE));
String ciri_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_CIRIKERUSAKAN));
String penyebab_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_PENYEBABKERUSAKAN));
String solusi_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_SOLUSIKERUSAKAN));
}
kode.setText(del_id);
nama.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_KATEGORIHARDWARE)));
ciri.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_CIRIKERUSAKAN)));
penyebab.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_PENYEBABKERUSAKAN)));
solusi.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_SOLUSIKERUSAKAN)));
cursor.close();
database.close();
new AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("")
.setPositiveButton("Save Changes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String nama1 = nama.getText().toString();
String ciri1 = ciri.getText().toString();
String penyebab1 = penyebab.getText().toString();
String solusi1 = solusi.getText().toString();
String kode1 = kode.getText().toString();
ContentValues values = new ContentValues();
values.put("kategorihardware", nama1);
values.put("cirikerusakan", ciri1);
values.put("penyebabkerusakan", penyebab1);
values.put("solusikerusakan", solusi1);
String where = "_id = ?";
String[] whereArgs = { kode1 };
SQLiteDatabase database = dbhelper.getWritableDatabase();
boolean updateSuccessful = database.update("mslh_cpu", values, where, whereArgs) > 0;
dialog.cancel();
adapter.clear();
readSampleData();
}
}).show();
adapter.notifyDataSetChanged();
countRecords_period();
}
// TAMBAH DATA KE TABEL
public void tambahRow(View view){
final Context context = view.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.tambahdatacirikerusakan, null, false);
final EditText tamono = (EditText) formElementsView.findViewById(R.id.namahardware);
final EditText cirino = (EditText) formElementsView.findViewById(R.id.cirikerusakan);
final EditText penyebabno = (EditText) formElementsView.findViewById(R.id.penyebabkerusakan);
final EditText solusino = (EditText) formElementsView.findViewById(R.id.solusikerusakan);
Button bt_tambah = (Button)view;
SQLiteDatabase database = dbhelper.getWritableDatabase();
new android.app.AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("Tambah data kerusakan")
.setPositiveButton("Simpan",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String tamono1 = tamono.getText().toString();
String cirino1 = cirino.getText().toString();
String penyebabno1 = penyebabno.getText().toString();
String solusino1 = solusino.getText().toString();
ContentValues values = new ContentValues();
values.put("kategorihardware", tamono1 );
values.put("cirikerusakan", cirino1);
values.put("penyebabkerusakan", penyebabno1);
values.put("solusikerusakan", solusino1);
SQLiteDatabase database = dbhelper.getWritableDatabase();
boolean createSuccessful = database.insert("mslh_cpu", null, values) > 0;
database.close();
if (createSuccessful) {
Toast.makeText(context, "Data kerusakan baru BERHASIH DISIMPAN", Toast.LENGTH_SHORT).show();
// listview.setSelection(adapter.getCount()-1);
} else {
Toast.makeText(context, "Data kerusakan baru GAGAL BERHASIH DISIMPAN", Toast.LENGTH_SHORT).show();
}
dialog.cancel();
adapter.clear();
readSampleData();
countRecords_period();
//ATUR LISTVIEW KE AKHIR
listview.setSelection(adapter.getCount()-1);
}
}).show();
adapter.notifyDataSetChanged();
countRecords_period();
}
}
2. CustomAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<String>{
int groupid;
ArrayList<String> records;
Context context;
public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
String[] row_items=records.get(position).split("__");
TextView t4id= (TextView) itemView.findViewById(R.id.kode);
t4id.setText("Kode Kerusakan : "+row_items[0]);
TextView nama= (TextView) itemView.findViewById(R.id.namahardware);
nama.setText("Nama Hardware : "+row_items[1]);
TextView ciri= (TextView) itemView.findViewById(R.id.cirikerusakan);
ciri.setText("Ciri Kerusakan : "+row_items[2]);
TextView penyebab= (TextView) itemView.findViewById(R.id.penyebabkerusakan);
penyebab.setText("Penyebab Kerusakan : "+row_items[3]);
TextView solusi= (TextView) itemView.findViewById(R.id.solusikerusakan);
solusi.setText("Solusi Kerusakan : "+row_items[4]);
Button bt_del_period=(Button)itemView.findViewById(R.id.bt_delete_period);
bt_del_period.setTag(row_items[0]);
Button bt_edit_period=(Button)itemView.findViewById(R.id.bt_edit_period);
bt_edit_period.setTag(row_items[0]);
return itemView;
}
}
3. SQLiteDB
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLiteDB extends SQLiteOpenHelper{
public static final int MYDATABASE_VERSION = 1;
public static final String MYDATABASE_NAME = "sistempakarkomputer";
public static final String MYDATABASE_TABLE = "mslh_cpu";
public static final String KEY_ID = "_id";
public static final String KEY_KATEGORIHARDWARE = "kategorihardware";
public static final String KEY_CIRIKERUSAKAN = "cirikerusakan";
public static final String KEY_PENYEBABKERUSAKAN = "penyebabkerusakan";
public static final String KEY_SOLUSIKERUSAKAN = "solusikerusakan";
//-------------DEKLARASI UNTUK MEMBUAT TABEL-------------//
private static final String SCRIPT_CREATE_TABLE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_KATEGORIHARDWARE + " text not null, "
+ KEY_CIRIKERUSAKAN + " text not null, "
+ KEY_PENYEBABKERUSAKAN + " text not null, "
+ KEY_SOLUSIKERUSAKAN + " text not null);";
//-------------DEKLARASI UNTUK MENGHAPUS TABEL-------------//
private static final String SCRIPT_DELETE_TABLE="DROP TABLE IF EXISTS " + MYDATABASE_TABLE;
//-------------DEKLARASI UNTUK MEMASUKKAN DATA DALAM TABEL-------------//
public SQLiteDB(Context context){
//BUAT DATABASE JIKA TIDAK ADA
super(context,MYDATABASE_NAME,null,MYDATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
//BUAT TABEL
db.execSQL(SCRIPT_CREATE_TABLE);
// db.execSQL(SCRIPT_MASUK_DATA1);
// db.execSQL(SCRIPT_MASUK_DATA2);
// db.execSQL(SCRIPT_MASUK_DATA3);
// db.execSQL(SCRIPT_MASUK_DATA4);
// db.execSQL(SCRIPT_MASUK_DATA5);
// db.execSQL(SCRIPT_MASUK_DATA6);
// db.execSQL(SCRIPT_MASUK_DATA7);
// db.execSQL(SCRIPT_MASUK_DATA8);
// db.execSQL(SCRIPT_MASUK_DATA9);
}
public void onUpgrade(SQLiteDatabase db,int olv,int newv){
db.execSQL(SCRIPT_DELETE_TABLE);
onCreate(db);
}
}
untuk hasil akhirnya seperti gambar dibawah ini:
sekian tutorial dari saya smoga bermanfaat.
0 Response to "Membuat Input Data pada android Studio"
Post a Comment