Mode Gelap

Recent in Fashion

Best Seller Books

Membuat Crud Sqlite Android Studio

list_row.xmlSebagai tampilan custom listview yg berisi nama & alamat.

    private String id, name, address;

    public Data(String id, String name, String address)

        this.name = name;

        this.address = address;

    public String getId()

    public void setId(String id)

    public String getName()

    public void setName(String name)

        this.name = name;

    public String getAddress()

        return address;

    public void setAddress(String address)

        this.address = address;

Buat group DbHelper.java, Class ini berfungsi sebagai pengeloala query SQL CRUD.

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper

    private static final int DATABASE_VERSION = 2;

    static final String DATABASE_NAME = "kuncorosqlite.db";

    public static final String TABLE_SQLite = "sqlite";

    public static final String COLUMN_ID = "id";

    public static final String COLUMN_NAME = "name";

    public static final String COLUMN_ADDRESS = "address";

    public DbHelper(Context context)

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    public void onCreate(SQLiteDatabase db)

        final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + TABLE_SQLite + " (" +

                COLUMN_ID + " INTEGER PRIMARY KEY autoincrement, " +

                COLUMN_NAME + " TEXT NOT NULL, " +

                COLUMN_ADDRESS + " TEXT NOT NULL" +

                " )";

        db.execSQL(SQL_CREATE_MOVIE_TABLE);

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SQLite);

    public ArrayListdanlt;HashMapdanlt;String, Stringdangt;> getAllData()

        ArrayList wordList;

        wordList = new ArrayListdanlt;HashMapdanlt;String, Stringdangt;>();

        String selectQuery = "SELECT * FROM " + TABLE_SQLite;

        SQLiteDatabase database = this.getWritableDatabase();

        Cursor cursor = database.rawQuery(selectQuery, null);

        if (cursor.moveToFirst())

                HashMap();

                map.put(COLUMN_ID, cursor.getString(0));

                map.put(COLUMN_NAME, cursor.getString(1));

                map.put(COLUMN_ADDRESS, cursor.getString(dua));

                wordList.add(map);

             while (cursor.moveToNext());

        Log.e("select sqlite ", "" + wordList);

        database.close();

        return wordList;

    public void insert(String name, String address)

        SQLiteDatabase database = this.getWritableDatabase();

        String queryValues = "INSERT INTO " + TABLE_SQLite + " (name, address) " +

                "VALUES ('" + name + "', '" + address + "')";

        Log.e("insert sqlite ", "" + queryValues);

        database.execSQL(queryValues);

        database.close();

    public void update(int id, String name, String address)

        SQLiteDatabase database = this.getWritableDatabase();

        String updateQuery = "UPDATE " + TABLE_SQLite + " SET "

                + COLUMN_NAME + "='" + name + "', "

                + COLUMN_ADDRESS + "='" + address + "'"

                + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";

        Log.e("update sqlite ", updateQuery);

        database.execSQL(updateQuery);

        database.close();

    public void delete(int id)

        SQLiteDatabase database = this.getWritableDatabase();

        String updateQuery = "DELETE FROM " + TABLE_SQLite + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";

        Log.e("update sqlite ", updateQuery);

        database.execSQL(updateQuery);

        database.close();

Buka class MainActivity.java dan tambahkan coding misalnya dibawah ini. Class ini berfungsi menampilkan data biodata pada listview. Floating Action Button di pakai buat memanggil Form diodata yang berisi nama dan alamat. Menekan usang listview akan menampilkan pilihan edit dan delete.

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.support.design.widget.FloatingActionButton;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.widget.AdapterView;

import android.widget.ListView;

public group MainActivity extends AppCompatActivity

    AlertDialog.Builder dialog;

    List itemList = new ArrayList

    DbHelper SQLite = new DbHelper(this);

    public static final String TAG_ID = "id";

    public static final String TAG_NAME = "name";

    public static final String TAG_ADDRESS = "address";

    protected void onCreate(Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        SQLite = new DbHelper(getApplicationContext());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        listView = (ListView) findViewById(R.id.list_view);

        fab.setOnClickListener(new View.OnClickListener()

            @Override

            public void onClick(View view)

                Intent intent = new Intent(MainActivity.this, AddEdit.class);

                startActivity(intent);

        adapter = new Adapter(MainActivity.this, itemList);

        listView.setAdapter(adapter);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()

            @Override

            public boolean onItemLongClick(final AdapterViewdanlt;?> parent, View view,

                                           final int position, long id)

                final String idx = itemList.get(position).getId();

                final String name = itemList.get(position).getName();

                final String address = itemList.get(position).getAddress();

                final CharSequence[] dialogitem = "Edit", "Delete";

                dialog = new AlertDialog.Builder(MainActivity.this);

                obrolan.setCancelable(true);

                obrolan.setItems(dialogitem, new DialogInterface.OnClickListener()

                    @Override

                    public void onClick(DialogInterface dialog, int which)

                        switch (which)

                            case 0:

                                Intent intent = new Intent(MainActivity.this, AddEdit.class);

                                intent.putExtra(TAG_ID, idx);

                                intent.putExtra(TAG_NAME, name);

                                intent.putExtra(TAG_ADDRESS, address);

                                startActivity(intent);

                                break;

                            case 1:

                                SQLite.delete(Integer.parseInt(idx));

                                itemList.clear();

Subscribe Our Newsletter

avatar
"Hidupku tertutup, terbukanya pas mandi aja."

Related Posts

0 Comment

Posting Komentar

Article Top Ads

Parallax Ads

Article Center Ads

Article Bottom Ads