i was search some tutorial for creating database in android but not success when i was running it..i was searching too about solve that problem, but i cant solve that
here sources:
MySQLHelper.java
package com.kiddies.tsel; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class mySqlHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "tsel.db"; private static final int DATABASE_VERSION = 1; //table name public static final String TABLE = "roaming"; //columns public static final String negara = "negara"; public static final String network = "network"; public static final String keindonesia = "indonesia"; public static final String local = "local"; public static final String kenegara = "kenegara"; public static final String terimatlp = "terimatlp"; public static final String sms = "sms"; public static final String gprs = "gprs"; public mySqlHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-Generated construct stub } public void onCreate(SQLiteDatabase db){ String sql = "create table"+TABLE+"(_id"+"integer primary key autoincrement," +negara+"text not null,"+network+"text not null,"+keindonesia+"text not null," +local+"text not null,"+kenegara+"text not null,"+terimatlp+"text not null," +sms+"text not null,"+gprs+"text not null);"; Log.d("Data", "onCretae:" + sql); db.execSQL(sql); ContentValues newValues = new ContentValues(); newValues.put("negara", "singapure"); newValues.put("network", "sinTel"); newValues.put("keindonesia","Rp.15000"); newValues.put("local", "Rp.7000"); newValues.put("kenegara", "Rp.18000"); newValues.put("terimatlp", "Rp.4000"); newValues.put("gprs", "Rp.129/Kb"); db.insert(TABLE, null, newValues); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ db.execSQL("DROP TABLE IF EXIST " + TABLE); onCreate(db); } }
TselActivity.java
package com.kiddies.tsel; import android.app.Activity; import android.os.Bundle; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.View; import android.widget.EditText; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class TselActivity extends Activity { /** Called when the activity is first created. */ mySqlHelper dbHelper; private TextView Jumlah; private Button but_Search; private EditText ed_Search; private TextView mytext; protected Cursor cursor; protected ListAdapter adapter; protected ListView List; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbHelper = new mySqlHelper(this); mytext = (TextView) findViewById(R.id.TextView01); but_Search = (Button) findViewById(R.id.But_Search); ed_Search = (EditText) findViewById(R.id.Ed_Search); List = (ListView) findViewById(R.id.ListView01); Jumlah = (TextView) findViewById(R.id.text_jumlah); } public void but_SearchClick(View v){ try { SQLiteDatabase db = dbHelper.getReadableDatabase(); cursor = db.rawQuery("SELECT _id, negara, network, keindonesia, local, kenegara, terimatlp, sms, gprs" + "FROM roaming WHERE negara LIKE'%"+ed_Search.getTextColors().toString()+"%'", null); adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[]{"negara"}, new int[] { android.R.id.text1}); List.setAdapter(adapter); cursor = db.rawQuery("select count(network) from roaming where negara LIKE'%" +ed_Search.getText().toString()+"%'"+cursor.getInt(0), null); cursor.moveToFirst(); Jumlah.setText("Jumlah:"+cursor.getInt(0)); } catch (Exception e) { mytext.setText(e.toString()); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/TextView01" android:text="Enter Countries:"/> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <EditText android:id="@+id/Ed_Search" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> <Button android:text="SEARCH" android:id="@+id/But_Search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="but_SearchClick"></Button> </LinearLayout> <TextView android:id="@+id/text_jumlah" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView> </LinearLayout>
help me for solve that problem..