Assalamualaikum Wr.Wb.
Eclipse adalah sebuah IDE (Integrated Development Environment) untuk mengembangkan perangkat lunak dan dapat dijalankan di semua platform (platform-independent) termasuk salah satunya adalah platform Android.
Kali ini kita akan belajar cara membuat daftar kontak menggunakan eclipse. Yang tampilan jadinya nanti seperti ini..
Oke langsung saja, mari kita bereksperimen.
1. Pertama-tama buat dulu projectnya dengan cara klik File -> New -> Android Application Project. Kemudian atur seperti gambar berikut (Sesuai dengan eclipse sobat).
2. Kemudian klik Next (biarkan default), kemudian Finish. Ok sampai disini kita sudah membuat Project kita.
Pada saat project pertama kali dibuat, ada 2 file penting yang langsung di buat yaitu
- MainActivity.java
- activity_main.xml
3. Sekarang mari kita otak-atik aplikasi ini. buka MainActivity.java, Lalu ketikkan seperti kode di bawah ini
package com.polimedia.inputnomor; import java.util.ArrayList; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends Activity { SqlHandler sqlHandler; ListView lvCustomList; EditText etName, etPhone; Button btnsubmit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvCustomList = (ListView) findViewById(R.id.lv_custom_list); etName = (EditText) findViewById(R.id.et_name); etPhone = (EditText) findViewById(R.id.et_phone); btnsubmit = (Button) findViewById(R.id.btn_submit); sqlHandler = new SqlHandler(this); showList(); btnsubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String name = etName.getText().toString(); String phoneNo = etPhone.getText().toString(); String query = "INSERT INTO PHONE_CONTACTS(name,phone) values ('" + name + "','" + phoneNo + "')"; sqlHandler.executeQuery(query); showList(); etName.setText(""); etPhone.setText(""); } }); } private void showList() { ArrayListcontactList = new ArrayList (); contactList.clear(); String query = "SELECT * FROM PHONE_CONTACTS "; Cursor c1 = sqlHandler.selectQuery(query); if (c1 != null & c1.getCount() != 0) { if (c1.moveToFirst()) { do { ContactListItems contactListItems = new ContactListItems(); contactListItems.setSlno(c1.getString(c1 .getColumnIndex("slno"))); contactListItems.setName(c1.getString(c1 .getColumnIndex("name"))); contactListItems.setPhone(c1.getString(c1 .getColumnIndex("phone"))); contactList.add(contactListItems); } while (c1.moveToNext()); } } c1.close(); ContactListAdapter contactListAdapter = new ContactListAdapter( MainActivity.this, contactList); lvCustomList.setAdapter(contactListAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
4. buka activity_main.xml kemudian ketikkan seperti kode di bawah
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:id="@+id/et_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" > </EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phone" /> <EditText android:id="@+id/et_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" > </EditText> </TableRow> </TableLayout> <LinearLayout android:id="@+id/LinearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/btn_submit" android:layout_width="80dp" android:layout_height="40dp" android:layout_marginLeft="40dp" android:text="@string/submit" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" > <TextView android:id="@+id/tv_slno" android:layout_width="50dp" android:layout_height="wrap_content" android:text="no" android:textColor="#000" /> <TextView android:id="@+id/tv_name" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/name" android:textColor="#000" /> <TextView android:id="@+id/tv_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/phone" android:textColor="#000" /> </LinearLayout> <ListView android:id="@+id/lv_custom_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > </ListView> </LinearLayout>
kurang lebih nanti tampilannya akan seperti ini, tapi ini belum jadi....
6. Lalu rubah kodenya, sehingga menjadi seperti di bawah
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_slno" android:layout_width="50dp" android:layout_height="wrap_content" android:text="no" android:textColor="#000" /> <TextView android:id="@+id/tv_name" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="TextView" android:textColor="#000" /> <TextView android:id="@+id/tv_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="TextView" android:textColor="#000" /> </LinearLayout>
7. Selanjutnya buat 4 class baru, dengan cara klik kanan pada package "com.ahmadsahidin.daftarview" dan pilih "new" lalu "class". Beri nama pada masing-masing class sebagai berikut
- ContactListAdapter.java
- ContactListItems.java
- SqlDbHelper.java
- SqlHandler.java
Sehingga sekarang kita punya 5 class baru termasuk MainActivity.java.
8. Buka "ContactListAdapter.java" Isikan kode berikut
package com.ahmadsahidin.daftarview; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ContactListAdapter extends BaseAdapter { Context context; ArrayListcontactList; public ContactListAdapter(Context context, ArrayList list) { this.context = context; contactList = list; } @Override public int getCount() { return contactList.size(); } @Override public Object getItem(int position) { return contactList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup arg2) { ContactListItems contactListItems = contactList.get(position); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.contact_list_row, null); } TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno); tvSlNo.setText(contactListItems.getSlno()); TextView tvName = (TextView) convertView.findViewById(R.id.tv_name); tvName.setText(contactListItems.getName()); TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone); tvPhone.setText(contactListItems.getPhone()); return convertView; } }
9. Buka "ContactListItems.java", Isikan kode berikut
package com.ahmadsahidin.daftarview; public class ContactListItems { String slno; String name; String phone; public String getSlno() { return slno; } public void setSlno(String slno) { this.slno = slno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
10. Buka "SqlDbHelper.java" Isikan kode berikut :
package com.ahmadsahidin.daftarview; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class SqlDbHelper extends SQLiteOpenHelper { public static final String DATABASE_TABLE = "PHONE_CONTACTS"; public static final String COLUMN1 = "slno"; public static final String COLUMN2 = "name"; public static final String COLUMN3 = "phone"; private static final String SCRIPT_CREATE_DATABASE = "create table " + DATABASE_TABLE + " (" + COLUMN1 + " integer primary key autoincrement, " + COLUMN2 + " text not null, " + COLUMN3 + " text not null);"; public SqlDbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(SCRIPT_CREATE_DATABASE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } }
11. Buka "SqlHandler.java" Isikan kode berikut :
package com.ahmadsahidin.daftarview; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class SqlHandler { public static final String DATABASE_NAME = "MY_DATABASE"; public static final int DATABASE_VERSION = 1; Context context; SQLiteDatabase sqlDatabase; SqlDbHelper dbHelper; public SqlHandler(Context context) { dbHelper = new SqlDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION); sqlDatabase = dbHelper.getWritableDatabase(); } public void executeQuery(String query) { try { if (sqlDatabase.isOpen()) { sqlDatabase.close(); } sqlDatabase = dbHelper.getWritableDatabase(); sqlDatabase.execSQL(query); } catch (Exception e) { System.out.println("DATABASE ERROR " + e); } } public Cursor selectQuery(String query) { Cursor c1 = null; try { if (sqlDatabase.isOpen()) { sqlDatabase.close(); } sqlDatabase = dbHelper.getWritableDatabase(); c1 = sqlDatabase.rawQuery(query, null); } catch (Exception e) { System.out.println("DATABASE ERROR " + e); } return c1; } }
12. Terakhir buka folder "res" kemudian pilih "values" dan klik pada "strings.xml" kemudian sesuaikan koenya seperti gambar berikut
13. Langkah terakhir, jalankan aplikasinya.
Tentang Penulis :
0 komentar:
Post a Comment