Tugas Mobile Programming - Praktikum 10 | Teknik Informatika UNPI

 NAMA                 : MUHAMMAD MUSLIM ABDUL JABBAAR

NIM                      : 217200035

PRODI                  : TEKNIK INFORMATIKA

MATKUL             : MOBILE PROGRAMMING 1


SQLite







  • activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="30dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Nama" />

    <EditText
        android:id="@+id/editTextNIM"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="NIM" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="30dp"
        android:text="Add" />

    <Button
        android:id="@+id/buttonList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:text="List" />

    <Button
        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete" />

</LinearLayout>

    <TextView
        android:id="@+id/textViewResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Nama | NIM"
        tools:textSize="20sp"
        android:textStyle="bold"
        android:textSize="20sp" />

</LinearLayout>

  • MainActivity.java
package com.opendebugger62.sqlitemuslim;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText editTextName, editTextNIM;
    private TextView textViewResult;
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextName = findViewById(R.id.editTextName);
        editTextNIM = findViewById(R.id.editTextNIM);
        textViewResult = findViewById(R.id.textViewResult);
        Button buttonAdd = findViewById(R.id.buttonAdd);
        Button buttonList = findViewById(R.id.buttonList);
        Button buttonDelete = findViewById(R.id.buttonDelete);

        databaseHelper = new DatabaseHelper(this);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editTextName.getText().toString();
                String nim = editTextNIM.getText().toString();

                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(DatabaseHelper.COLUMN_NAME, name);
                values.put(DatabaseHelper.COLUMN_NIM, nim);
                db.insert(DatabaseHelper.TABLE_NAME, null, values);

                editTextName.setText("");
                editTextNIM.setText("");
            }
        });

        buttonList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = databaseHelper.getReadableDatabase();
                Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME, null);
                StringBuilder stringBuilder = new StringBuilder();
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME));
                    String nim = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NIM));
                    stringBuilder.append(" ").append(name).append(" | ").append(nim).append("\n");
                }
                cursor.close();
                textViewResult.setText(stringBuilder.toString());
            }
        });

        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                db.delete(DatabaseHelper.TABLE_NAME, null, null);
                textViewResult.setText("");
            }
        });
    }
}

  • DatabaseHelper.java
package com.opendebugger62.sqlitemuslim;

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

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "student.db";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "student";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_NIM = "nim";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " TEXT, " +
                COLUMN_NIM + " TEXT)";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}




Komentar

Postingan populer dari blog ini

Tugas Mobile Programming - Praktikum 5 | Teknik Informatika UNPI

Tugas Mobile Programming - Praktikum 8 | Teknik Informatika UNPI

Tugas Mobile Programming - Praktikum 9 | Teknik Informatika UNPI