Input Data ke Database sqlite

1.  main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@android:id/tabhost"
 android:layout_width="fill_parent"
 android:background="@drawable/bg"
 android:layout_height="fill_parent">
 <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TabWidget android:id="@android:id/tabs"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
  />
  <FrameLayout android:id="@android:id/tabcontent"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView android:id="@+id/almag"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
    />
   </LinearLayout>
   <TableLayout android:id="@+id/details"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:paddingTop="4px"
    >
    <TableRow>
     <TextView android:text="Nama:" />
     <EditText android:id="@+id/nama" />
    </TableRow>
    <TableRow>
        <TextView android:text="NIM:" />
     <EditText android:id="@+id/alamat"
         android:inputType="number"/>
    </TableRow>
    <TableRow>
     <TextView android:text="Jekel:" />
     <RadioGroup android:id="@+id/jekel">
      <RadioButton android:id="@+id/pria"
       android:text="Pria"
      />
      <RadioButton android:id="@+id/perempuan"
       android:text="Perempuan"
      />
     </RadioGroup>
    </TableRow>
    <TableRow>
     <TextView android:text="Jurusan:" />
     <EditText android:id="@+id/hp"
      android:gravity="top"
      android:scrollHorizontally="false"
   
     />
    </TableRow>
    <Button android:id="@+id/save"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Save"
    />
   </TableLayout>
  </FrameLayout>
 </LinearLayout>
</TabHost>

2. row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:padding="4px"
 >
 <ImageView android:id="@+id/icon"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_alignParentTop="true"
  android:layout_alignParentBottom="true"
  android:layout_marginRight="4px"
 />
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  >
  <TextView android:id="@+id/title"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:gravity="center_vertical"
   android:textStyle="bold"
   android:singleLine="true"
   android:ellipsize="end"
  />
  <TextView android:id="@+id/alamat"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:gravity="center_vertical"
   android:singleLine="true"
   android:ellipsize="end"
  />
 </LinearLayout>
</LinearLayout>

selanjutnya kita membuat javanya yah agar aplikasi ini bisa di jalankan buat saja class

4. Database2.java

package com.wilis.database2;

import com.wilis.database2.R;

import android.app.TabActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;

public class Database2 extends TabActivity {
 Cursor model=null;
 AlmagAdapter adapter=null;
 EditText nama=null;
 EditText alamat=null;
 EditText hp=null;
 RadioGroup jekel=null;
 AlmagHelper helper=null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  helper=new AlmagHelper(this);

  nama=(EditText)findViewById(R.id.nama);
  alamat=(EditText)findViewById(R.id.alamat);
  hp=(EditText)findViewById(R.id.hp);
  jekel=(RadioGroup)findViewById(R.id.jekel);

  Button save=(Button)findViewById(R.id.save);

  save.setOnClickListener(onSave);

  ListView list=(ListView)findViewById(R.id.almag);

  model=helper.getAll();
  startManagingCursor(model);
  adapter=new AlmagAdapter(model);
list.setAdapter(adapter);

  TabHost.TabSpec spec=getTabHost().newTabSpec("tag1");

  spec.setContent(R.id.almag);
  spec.setIndicator("List", getResources().getDrawable(R.drawable.list));
  getTabHost().addTab(spec);

  spec=getTabHost().newTabSpec("tag2");
  spec.setContent(R.id.details);
  spec.setIndicator("Details",
getResources().getDrawable(R.drawable.details));

  getTabHost().addTab(spec);

  getTabHost().setCurrentTab(0);

  list.setOnItemClickListener(onListClick);
 }

 @Override
 public void onDestroy() {
  super.onDestroy();

  helper.close();
 }

 private View.OnClickListener onSave=new View.OnClickListener() {
  public void onClick(View v) {
   String type=null;

   switch (jekel.getCheckedRadioButtonId()) {
    case R.id.pria:
     type="Pria";
     break;
    case R.id.perempuan:
     type="Perempuan";
     break;
   }


 helper.insert(nama.getText().toString(),alamat.getText().toString(),type,hp.getText().toString());
   model.requery();
  }
 };

 private AdapterView.OnItemClickListener onListClick=new
AdapterView.OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent,
          
  View view, int position,
          
  long id) {
   model.moveToPosition(position);
   nama.setText(helper.getNama(model));
   alamat.setText(helper.getAlamat(model));
   hp.setText(helper.getHp(model));

   if (helper.getJekel(model).equals("Pria")) {
    jekel.check(R.id.pria);
   }
   else if (helper.getJekel(model).equals("Perempuan")) {
    jekel.check(R.id.perempuan);
   }


   getTabHost().setCurrentTab(1);
  }
 };

 class AlmagAdapter extends CursorAdapter {
  AlmagAdapter(Cursor c) {
   super(Database2.this, c);
  }

  @Override
  public void bindView(View row, Context ctxt,
         
Cursor c) {
   AlmagHolder holder=(AlmagHolder)row.getTag();

   holder.populateFrom(c, helper);
  }

  @Override
  public View newView(Context ctxt, Cursor c,
         
ViewGroup parent) {
   LayoutInflater inflater=getLayoutInflater();
   View row=inflater.inflate(R.layout.row, parent, false);
   AlmagHolder holder=new AlmagHolder(row);

   row.setTag(holder);

   return(row);
  }
 }

 static class AlmagHolder {
  private TextView nama=null;
  private TextView alamat=null;
  private ImageView icon=null;
  private View row=null;

  AlmagHolder(View row) {
   this.row=row;
 
   nama=(TextView)row.findViewById(R.id.title);
   alamat=(TextView)row.findViewById(R.id.alamat);
   icon=(ImageView)row.findViewById(R.id.icon);
  }

  void populateFrom(Cursor c, AlmagHelper helper) {
   nama.setText(helper.getNama(c));
   alamat.setText(helper.getAlamat(c));

   if (helper.getJekel(c).equals("Pria")) {
    icon.setImageResource(R.drawable.pria);
   }
   else if (helper.getJekel(c).equals("Perempuan")) {
    icon.setImageResource(R.drawable.perempuan);
   }

  }
 }
}

5. AlmagHelper.java

package com.wilis.database2;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;


class AlmagHelper extends SQLiteOpenHelper {
 private static final String DATABASE_NAME="addressmanager.db";
 private static final int SCHEMA_VERSION=1;

 public AlmagHelper(Context context) {
  super(context, DATABASE_NAME, null, SCHEMA_VERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT,nama TEXT, alamat TEXT, jekel TEXT, hp TEXT);");
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // no-op, since will not be called until 2nd schema
  // version exists
 }
 public Cursor getAll() {
      return(getReadableDatabase()
          .rawQuery("SELECT _id, nama, alamat, jekel,hp FROM almag ORDER BY nama",
               null));
     }
 
     public void insert(String nama, String alamat,
               String jekel,
    String hp) {
      ContentValues cv=new ContentValues();
      
      cv.put("nama", nama);
      cv.put("alamat", alamat);
      cv.put("jekel", jekel);
      cv.put("hp", hp);
   
      getWritableDatabase().insert("almag", "name", cv);
     }
 
     public String getNama(Cursor c) {
      return(c.getString(1));
     }
 
     public String getAlamat(Cursor c) {
      return(c.getString(2));
     }
 
     public String getJekel(Cursor c) {
      return(c.getString(3));
     }
 
     public String getHp(Cursor c) {
      return(c.getString(4));
     }
    }


jika tidak ada yang error langsung saja di running aplikasinya dan hasilnya akan seperti ini..




Komentar

Postingan populer dari blog ini

TEKS TO SPEACH

Spinner

FROM LOGIN