Public Koneksi As New ADODB.Connection
Public Sub KoneksiDatabase()
On Error GoTo konekErr
If Koneksi.State = 1 Then Koneksi.Close
Koneksi.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\DBNILAI.mdb;Persist Security Info=False"
Exit Sub
konekErr:
MsgBox "Gagal menghubungkan ke Database ! Kesalahan pada : " & Err.Description, vbCritical, "Peringatan"
End Sub
Private Sub Form_Load()
Dim kodecari As String
kodecari = "M1"
KoneksiDatabase
Adodc1.ConnectionString = Koneksi.ConnectionString
Adodc1.RecordSource = "SELECT * FROM MHS WHERE KodeMHS = '" & kodecari & "' "
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
End Sub
Silakan download
Senin, 16 Oktober 2017
Sabtu, 14 Oktober 2017
Belajar Adapter (Daftar Kata) di Kamus
Layout daftarkata.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text = "Inggris"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:text = "Indonesia"
android:textSize="20sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView2"
android:text = "Jerman"
android:textSize="20sp" />
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight = "1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"/>
</RelativeLayout>
Layout untuk setiap item di ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="5sp"
android:paddingTop="5sp" >
<TextView
android:id="@+id/inggris"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/indonesia"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/inggris" />
<TextView
android:id="@+id/jerman"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/indonesia" />
</RelativeLayout>
Class Controller untuk DaftaKata.java
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class DaftarKata extends Activity {
private DatabaseHelper dbhelper;
private SQLiteDatabase db = null;
private ListView listContent = null;
private Cursor kamusCursor = null;
CustomCursorAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbhelper = new DatabaseHelper(this);
setContentView(R.layout.daftarkata);
listContent = (ListView) findViewById(R.id.list1);
isDataListView();
}
private void isDataListView() {
try {
db = dbhelper.getWritableDatabase();
kamusCursor = db.query("kamus", new String[] { "_id", "inggris",
"indonesia", "jerman" }, "_id>0", null, null, null, null);
/*
* Create an array to specify the fields we want to display in the
* list (only the 'inggris,indonesia,jerman' column in this case)
*/
String[] from = new String[] { "inggris", "indonesia", "jerman" };
/*
* and an array of the fields we want to bind those fieiplds to (in
* this case just the textView 'inggris,indonesia,jerman' from our new row.xml
* layout above)
*/
int[] to = new int[] { R.id.inggris, R.id.indonesia, R.id.jerman };
/* Now create a simple cursor adapter.. */
adapter = new CustomCursorAdapter(this, R.layout.row, kamusCursor,from, to);
listContent.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null && db.isOpen()) {
db.close();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
kamusCursor.close();
} catch (Exception e) {
}
}
protected class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super (context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d("NewView", "*****xxx");
View v = inflater.inflate(R.layout.row, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
// 1 is the column where you're getting your data from
String inggris = c.getString(1);
String jerman = c.getString(3);
String indonesia = c.getString(2);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.inggris);
TextView des_text = (TextView) v.findViewById(R.id.jerman);
TextView id_text = (TextView) v.findViewById(R.id.indonesia);
des_text.setText(jerman);
id_text.setText(indonesia);
if (name_text != null) {
name_text.setText(inggris);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text = "Inggris"
android:textSize="20sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:text = "Indonesia"
android:textSize="20sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView2"
android:text = "Jerman"
android:textSize="20sp" />
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight = "1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"/>
</RelativeLayout>
Layout untuk setiap item di ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="5sp"
android:paddingTop="5sp" >
<TextView
android:id="@+id/inggris"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/indonesia"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/inggris" />
<TextView
android:id="@+id/jerman"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/indonesia" />
</RelativeLayout>
Class Controller untuk DaftaKata.java
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class DaftarKata extends Activity {
private DatabaseHelper dbhelper;
private SQLiteDatabase db = null;
private ListView listContent = null;
private Cursor kamusCursor = null;
CustomCursorAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbhelper = new DatabaseHelper(this);
setContentView(R.layout.daftarkata);
listContent = (ListView) findViewById(R.id.list1);
isDataListView();
}
private void isDataListView() {
try {
db = dbhelper.getWritableDatabase();
kamusCursor = db.query("kamus", new String[] { "_id", "inggris",
"indonesia", "jerman" }, "_id>0", null, null, null, null);
/*
* Create an array to specify the fields we want to display in the
* list (only the 'inggris,indonesia,jerman' column in this case)
*/
String[] from = new String[] { "inggris", "indonesia", "jerman" };
/*
* and an array of the fields we want to bind those fieiplds to (in
* this case just the textView 'inggris,indonesia,jerman' from our new row.xml
* layout above)
*/
int[] to = new int[] { R.id.inggris, R.id.indonesia, R.id.jerman };
/* Now create a simple cursor adapter.. */
adapter = new CustomCursorAdapter(this, R.layout.row, kamusCursor,from, to);
listContent.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null && db.isOpen()) {
db.close();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
kamusCursor.close();
} catch (Exception e) {
}
}
protected class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super (context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d("NewView", "*****xxx");
View v = inflater.inflate(R.layout.row, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
// 1 is the column where you're getting your data from
String inggris = c.getString(1);
String jerman = c.getString(3);
String indonesia = c.getString(2);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.inggris);
TextView des_text = (TextView) v.findViewById(R.id.jerman);
TextView id_text = (TextView) v.findViewById(R.id.indonesia);
des_text.setText(jerman);
id_text.setText(indonesia);
if (name_text != null) {
name_text.setText(inggris);
}
}
}
}
Senin, 09 Oktober 2017
Android SQLite
Class Kamus
public class Kamus {
private String istilah;
private String arti;
private String baru;
public String getIstilah() {
return istilah;
}
public void setIstilah(String istilah) {
this.istilah = istilah;
}
public String getArti() {
return arti;
}
public void setArti(String arti) {
this.arti = arti;
}
public String getBaru() {
return baru;
}
public void setBaru(String baru) {
this.baru = baru;
}
@Override
public String toString() {
return this.istilah;
}
}
public class Kamus {
private String istilah;
private String arti;
private String baru;
public String getIstilah() {
return istilah;
}
public void setIstilah(String istilah) {
this.istilah = istilah;
}
public String getArti() {
return arti;
}
public void setArti(String arti) {
this.arti = arti;
}
public String getBaru() {
return baru;
}
public void setBaru(String baru) {
this.baru = baru;
}
@Override
public String toString() {
return this.istilah;
}
}
Class DatabaseHelper
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseHelper extends SQLiteAssetHelper {
private static final String DB_NAME = "db_kamus10";
private static final int DB_VER = 1;
private static final String TB_DATA = "tb_kamus";
public static final String COL_ID = "_id";
public static final String COL_ISTILAH = "istilah";
public static final String COL_ARTI = "arti";
public static final String COL_BARU = "baru";
private static DatabaseHelper dbInstance;
private static SQLiteDatabase db;
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VER);
}
public static DatabaseHelper getInstance(Context context) {
if (dbInstance == null) {
dbInstance = new DatabaseHelper(context);
db = dbInstance.getWritableDatabase();
}
return dbInstance;
}
@Override
public synchronized void close() {
super.close();
if (dbInstance != null) {
dbInstance.close();
}
}
public List<Kamus> getAllKamus() {
List<Kamus> lisKamus = new ArrayList<Kamus>();
Cursor cursor = db.query(TB_DATA, new String[] { COL_ID, COL_ID,
COL_ARTI, COL_BARU, COL_ISTILAH}, null, null, null, null, COL_ISTILAH);
if (cursor.getCount() >= 1) {
cursor.moveToFirst();
do {
Kamus kamus = new Kamus();
kamus.setArti(cursor.getString(cursor.getColumnIndexOrThrow(COL_ARTI)));
kamus.setBaru(cursor.getString(cursor.getColumnIndexOrThrow(COL_BARU)));
kamus.setIstilah(cursor.getString(cursor.getColumnIndexOrThrow(COL_ISTILAH)));
lisKamus.add(kamus);
} while (cursor.moveToNext());
}
return lisKamus;
}
}
Class MainActivity
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity implements TextWatcher, OnItemClickListener {
private EditText search;
private ListView lv;
private DatabaseHelper dbHelper;
private ArrayAdapter<Kamus> adapter;
private List<Kamus> listKamus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv_data);
lv.setEmptyView(findViewById(R.id.empty));
search = (EditText) findViewById(R.id.search);
dbHelper = DatabaseHelper.getInstance(this);
setData();
search.addTextChangedListener(this);
lv.setOnItemClickListener(this);
}
private void setData() {
listKamus = dbHelper.getAllKamus();
adapter = new ArrayAdapter<Kamus>(this,
android.R.layout.simple_expandable_list_item_1, listKamus);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Bundle b = new Bundle();
b.putString("istilah", adapter.getItem(position).getIstilah());
b.putString("arti", adapter.getItem(position).getArti());
b.putString("baru", adapter.getItem(position).getBaru());
Intent i = new Intent(this, ArtiActivity.class);
i.putExtras(b);
startActivity(i);
finish();
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s.toString());
}
}
Class ArtiActivity
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class ArtiActivity extends Activity {
private TextView txtBaru, txtArti, txtIstilah;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arti);
Bundle b = getIntent().getExtras();
txtIstilah = (TextView) findViewById(R.id.istilah);
txtArti = (TextView) findViewById(R.id.arti);
txtBaru = (TextView) findViewById(R.id.baru);
txtIstilah.setText(b.getString("istilah"));
txtArti.setText(b.getString("arti"));
txtBaru.setText(b.getString("baru"));
}
}
Layout activity_arti
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ArtiActivity" >
<TextView
android:id="@+id/istilah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Istilah" />
<TextView
android:id="@+id/garis"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/istilah"
android:background="#000" />
<TextView
android:id="@+id/baru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/garis"
android:text="Baru" />
<TextView
android:id="@+id/arti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/baru"
android:text="Arti" />
</RelativeLayout>
Senin, 02 Oktober 2017
Materi SetOnClickListener
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button keluar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
keluar = (Button) findViewById(R.id.exitBtn1);
keluar.setOnClickListener(this);
Button linearLayout = (Button) findViewById(R.id.ButtonLinear);
linearLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View linear) {
Intent myIntent = new Intent(linear.getContext(),
LinearActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button relativeLayout = (Button) findViewById(R.id.ButtonRelative);
relativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View relative) {
Intent myIntent = new Intent(relative.getContext(),
RelativeActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button tableLayout = (Button) findViewById(R.id.ButtonTable);
tableLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View table) {
Intent myIntent = new Intent(table.getContext(),
TableActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
public void onClick(View clicked) {
switch (clicked.getId()) {
case R.id.exitBtn1: exit(); break;
}
}
private void exit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah Kamu Benar-Benar ingin keluar?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
}).show();
}
}
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button keluar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
keluar = (Button) findViewById(R.id.exitBtn1);
keluar.setOnClickListener(this);
Button linearLayout = (Button) findViewById(R.id.ButtonLinear);
linearLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View linear) {
Intent myIntent = new Intent(linear.getContext(),
LinearActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button relativeLayout = (Button) findViewById(R.id.ButtonRelative);
relativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View relative) {
Intent myIntent = new Intent(relative.getContext(),
RelativeActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button tableLayout = (Button) findViewById(R.id.ButtonTable);
tableLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View table) {
Intent myIntent = new Intent(table.getContext(),
TableActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
public void onClick(View clicked) {
switch (clicked.getId()) {
case R.id.exitBtn1: exit(); break;
}
}
private void exit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah Kamu Benar-Benar ingin keluar?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.cancel();
}
}).show();
}
}
Langganan:
Komentar (Atom)