Nah barusan ane iseng-iseng bikin aplikasi sederhana dengan Database SQLite bawaan Android, apilikasi nya simpel sih, 10Tips Untuk Pendaki Pemula...kenapa ane bikin app ini? kepikiran tadi habis baca-baca sebuah web yang berisi tips-tips pendakian kegunung (jadi kangen ke gunung euy, pengen lihat rembulan..:p ), so, ya lumayanlah buat nambah posting di blog ini, dari pada ini blog mati kelaparan karna tak di isi..:p
Oke lanjut and langsung aje, Berikut tampilan appnya dari emulator di android ;
Berikut file layout XMLnya :
1. main.xml
2. list_tips.xml
3. detail.xml
Berikut file JAVAnya ;
1. TipsAdapter.java
package com.tipspemula.yonandroid; import java.util.ArrayList; import java.util.HashMap; import com.tipspemula.yonandroid.adapter.TipsAdapter; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends ListActivity { public static final String YA_ID = "id"; public static final String YA_JUDUL = "judul"; ArrayList> daftar_tips = new ArrayList >(); private SQLiteDatabase db = null; private DataTips dataDB = null; private Cursor cursor = null; private HashMap mapp; private ListView list; private TipsAdapter adap; private String i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); dataDB = new DataTips(this); db = dataDB.getWritableDatabase(); dataDB.createTable(db); dataDB.generateData(db); getTips(); } // TODO Auto-generated method stub private void getTips() { try { cursor = db.rawQuery("SELECT * " + "FROM tips", null); if (cursor.getCount() > 0) { int indexID = cursor.getColumnIndex("id"); int indexJudul = cursor.getColumnIndex("judul"); cursor.moveToFirst(); do { String ID = cursor.getString(indexID); String Judul = cursor.getString(indexJudul); mapp = new HashMap (); mapp.put(YA_ID, ID); mapp.put(YA_JUDUL, Judul); daftar_tips.add(mapp); cursor.moveToNext(); } while (!cursor.isAfterLast()); } } finally { if (cursor != null) { cursor.close(); } this.adapter_listview(); } } // TODO Auto-generated method stub private void adapter_listview() { list = getListView(); adap = new TipsAdapter(this, daftar_tips); list.setAdapter(adap); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view,int position, long id) { i = ((TextView) view.findViewById(R.id.id)).getText().toString(); Intent in = new Intent(MainActivity.this, DetailTips.class); in.putExtra("ID", i); startActivity(in); } }); } }
2. MainActivity.java
package com.tipspemula.yonandroid; import java.util.ArrayList; import java.util.HashMap; import com.tipspemula.yonandroid.adapter.TipsAdapter; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends ListActivity { public static final String YA_ID = "id"; public static final String YA_JUDUL = "judul"; ArrayList> daftar_tips = new ArrayList >(); private SQLiteDatabase db = null; private DataTips dataDB = null; private Cursor cursor = null; private HashMap mapp; private ListView list; private TipsAdapter adap; private String i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); dataDB = new DataTips(this); db = dataDB.getWritableDatabase(); dataDB.createTable(db); dataDB.generateData(db); getTips(); } // TODO Auto-generated method stub private void getTips() { try { cursor = db.rawQuery("SELECT * " + "FROM tips", null); if (cursor.getCount() > 0) { int indexID = cursor.getColumnIndex("id"); int indexJudul = cursor.getColumnIndex("judul"); cursor.moveToFirst(); do { String ID = cursor.getString(indexID); String Judul = cursor.getString(indexJudul); mapp = new HashMap (); mapp.put(YA_ID, ID); mapp.put(YA_JUDUL, Judul); daftar_tips.add(mapp); cursor.moveToNext(); } while (!cursor.isAfterLast()); } } finally { if (cursor != null) { cursor.close(); } this.adapter_listview(); } } // TODO Auto-generated method stub private void adapter_listview() { list = getListView(); adap = new TipsAdapter(this, daftar_tips); list.setAdapter(adap); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view,int position, long id) { i = ((TextView) view.findViewById(R.id.id)).getText().toString(); Intent in = new Intent(MainActivity.this, DetailTips.class); in.putExtra("ID", i); startActivity(in); } }); } }
3. Ada dua file Java lagi yaitu; DataTips.java dan Detail.java, silahkan download aja file lengkapnya disini
Berikut Tampilannya :
List Data
Detail Data
Oke, sekian aje,.. and happy coding..
0 komentar:
Post a Comment