Newest Post
Aplikasi Daftar Handphone dengan listview menggunakan sqlite aplikasi
ini saya buat menggunakan android studio. dimana ada 3 activity yang
pertama form dari Login dan yang ke 2 ada form Register dan yang ke 3
adalah listviewnya. disini dibagian form loginnya menggunakan database
yaitu sqlite setelah itu kita masuk ke listview handphone.
langkah - langkah membuat project baru di android studio
3. Masukan Nama, Tentukan Lokasi Penyimpanan, Lalu Pilih Minimum API Level. kemudian Finish
Setelah Klik Finish Maka akan tampil depan / XML dan Java Class nya
4. Membuat DatabaseHelper
4.1 Source Code DataBaseHelper.Java
5. Membuat Login.Java
5.1 Source Code Login.Java
5.2 Membuat Login.XML
5.3 Source Code Login.XML
6. Membuat Register.Java
6.1 Source Code Register.Java
6.2 Membuat Register.XML
6.3 Source Code Register.XML
7.Membuat ListView.Java
7.1 Source Code ListView.Java
7.2 Membuat Layout Dari Listview
7.3 Source Code ListView.XML
8. Membuat Layout_lv.XML
8.1 Source Code Layout_lv.XML
maka sudah selesai untuk pembuatan aplikasi Daftar Hp Menggunakan Sqlite. Sekarang kita akan masuk ke tahap uji coba atau kita coba Run Aplikasinya.
Yang Pertama Kita masuk Ke form Login, sebelumnya kita harus daftar terlebih dahulu.
Dan dibawah ini adalah form untuk Daftar Atau register untuk masuk ke aplikasi
Setelah sudah daftar kita akan login ke aplikasinya. dan dibawah ini adalah tampilan setelah login
Berikut Adalah Video Hasil Running dari aplikasinya..
Sekian penjelasan dari aplikasi daftar handphone menggunakan sqlite. sekiranya ada pertanyaan silakan isi di kolom komentar.. Terimakasih GBU... :)
Judul Project Dan Penjelasan
langkah - langkah membuat project baru di android studio
- Buka Aplikasi Android Studio
- Buat Project baru dengan memilih template Empty kemudian klik Next
Pemilihan Template |
3. Masukan Nama, Tentukan Lokasi Penyimpanan, Lalu Pilih Minimum API Level. kemudian Finish
Menentukan Nama |
4. Membuat DatabaseHelper
DataBaseHelper |
package com.example.kelvinn;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="register.db";
public static final String TABLE_NAME ="registeruser";
public static final String COL_1 ="ID";
public static final String COL_2 ="username";
public static final String COL_3 ="password";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long addUser(String user, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username",user);
contentValues.put("password",password);
long res = db.insert("registeruser",null,contentValues);
db.close();
return res;
}
public boolean checkUser(String username, String password){
String[] columns = { COL_1 };
SQLiteDatabase db = getReadableDatabase();
String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
String[] selectionArgs = { username, password };
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
int count = cursor.getCount();
cursor.close();
db.close();
if(count>0)
return true;
else
return false;
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="register.db";
public static final String TABLE_NAME ="registeruser";
public static final String COL_1 ="ID";
public static final String COL_2 ="username";
public static final String COL_3 ="password";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long addUser(String user, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username",user);
contentValues.put("password",password);
long res = db.insert("registeruser",null,contentValues);
db.close();
return res;
}
public boolean checkUser(String username, String password){
String[] columns = { COL_1 };
SQLiteDatabase db = getReadableDatabase();
String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
String[] selectionArgs = { username, password };
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
int count = cursor.getCount();
cursor.close();
db.close();
if(count>0)
return true;
else
return false;
}
}
5. Membuat Login.Java
Login |
package com.example.kelvinn;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
Button mButtonLogin;
TextView mTextViewRegister;
DatabaseHelper db;
ViewGroup progressView;
protected boolean isProgressShowing = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mButtonLogin = (Button)findViewById(R.id.button_login);
mTextViewRegister = (TextView)findViewById(R.id.textview_register);
mTextViewRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(registerIntent);
}
});
mButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
Boolean res = db.checkUser(user, pwd);
if(res == true)
{
Intent HomePage = new Intent(LoginActivity.this,MainActivity.class);
startActivity(HomePage);
}
else
{
Toast.makeText(LoginActivity.this,"Login Error",Toast.LENGTH_SHORT).show();
}
}
});
}
public void showProgressingView() {
// if (!isProgressShowing) {
//View view=findViewById(R.id.progressBar1);
// view.bringToFront();
// }
}
public void hideProgressingView() {
View v = this.findViewById(android.R.id.content).getRootView();
ViewGroup viewGroup = (ViewGroup) v;
viewGroup.removeView(progressView);
isProgressShowing = false;
}
}
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
Button mButtonLogin;
TextView mTextViewRegister;
DatabaseHelper db;
ViewGroup progressView;
protected boolean isProgressShowing = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mButtonLogin = (Button)findViewById(R.id.button_login);
mTextViewRegister = (TextView)findViewById(R.id.textview_register);
mTextViewRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(registerIntent);
}
});
mButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
Boolean res = db.checkUser(user, pwd);
if(res == true)
{
Intent HomePage = new Intent(LoginActivity.this,MainActivity.class);
startActivity(HomePage);
}
else
{
Toast.makeText(LoginActivity.this,"Login Error",Toast.LENGTH_SHORT).show();
}
}
});
}
public void showProgressingView() {
// if (!isProgressShowing) {
//View view=findViewById(R.id.progressBar1);
// view.bringToFront();
// }
}
public void hideProgressingView() {
View v = this.findViewById(android.R.id.content).getRootView();
ViewGroup viewGroup = (ViewGroup) v;
viewGroup.removeView(progressView);
isProgressShowing = false;
}
}
5.2 Membuat Login.XML
5.3 Source Code Login.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="LoginActivity"
android:orientation="vertical"
android:id="@+id/container"
android:background="#123456"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="127dp"
android:layout_marginTop="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@android:color/transparent"
android:gravity="center_horizontal"
android:orientation="vertical">
<EditText
android:id="@+id/edittext_username"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:hint="@string/username"
android:paddingLeft="2dp" />
<EditText
android:id="@+id/edittext_password"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:inputType="textPassword"
android:hint="@string/password" />
</LinearLayout>
<Button
android:id="@+id/button_login"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#04ea00"
android:textColor="#ffffff"
android:text="@string/login"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Sudah Punya Akun?"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:text="Daftar"
android:textColor="#04ea00"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<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="LoginActivity"
android:orientation="vertical"
android:id="@+id/container"
android:background="#123456"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="127dp"
android:layout_marginTop="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@android:color/transparent"
android:gravity="center_horizontal"
android:orientation="vertical">
<EditText
android:id="@+id/edittext_username"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:hint="@string/username"
android:paddingLeft="2dp" />
<EditText
android:id="@+id/edittext_password"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:inputType="textPassword"
android:hint="@string/password" />
</LinearLayout>
<Button
android:id="@+id/button_login"
android:layout_width="190dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#04ea00"
android:textColor="#ffffff"
android:text="@string/login"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Sudah Punya Akun?"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:text="Daftar"
android:textColor="#04ea00"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
6. Membuat Register.Java
6.1 Source Code Register.Java
package com.example.kelvinn;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class RegisterActivity extends AppCompatActivity {
DatabaseHelper db;
EditText mTextUsername;
EditText mTextPassword;
EditText mTextCnfPassword;
Button mButtonRegister;
TextView mTextViewLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mTextCnfPassword = (EditText)findViewById(R.id.edittext_cnf_password);
mButtonRegister = (Button)findViewById(R.id.button_register);
mTextViewLogin = (TextView)findViewById(R.id.textview_login);
mTextViewLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent LoginIntent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(LoginIntent);
}
});
mButtonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
String cnf_pwd = mTextCnfPassword.getText().toString().trim();
if(pwd.equals(cnf_pwd)){
long val = db.addUser(user,pwd);
if(val > 0){
Toast.makeText(RegisterActivity.this,"Berhasil Mendaftar",Toast.LENGTH_SHORT).show();
Intent moveToLogin = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(moveToLogin);
}
else{
Toast.makeText(RegisterActivity.this,"Registeration Error",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(RegisterActivity.this,"Password is not matching",Toast.LENGTH_SHORT).show();
}
}
});
}
}
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class RegisterActivity extends AppCompatActivity {
DatabaseHelper db;
EditText mTextUsername;
EditText mTextPassword;
EditText mTextCnfPassword;
Button mButtonRegister;
TextView mTextViewLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mTextCnfPassword = (EditText)findViewById(R.id.edittext_cnf_password);
mButtonRegister = (Button)findViewById(R.id.button_register);
mTextViewLogin = (TextView)findViewById(R.id.textview_login);
mTextViewLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent LoginIntent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(LoginIntent);
}
});
mButtonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
String cnf_pwd = mTextCnfPassword.getText().toString().trim();
if(pwd.equals(cnf_pwd)){
long val = db.addUser(user,pwd);
if(val > 0){
Toast.makeText(RegisterActivity.this,"Berhasil Mendaftar",Toast.LENGTH_SHORT).show();
Intent moveToLogin = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(moveToLogin);
}
else{
Toast.makeText(RegisterActivity.this,"Registeration Error",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(RegisterActivity.this,"Password is not matching",Toast.LENGTH_SHORT).show();
}
}
});
}
}
6.2 Membuat Register.XML
6.3 Source Code Register.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"
android:background="@color/colorPrimaryDark"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="RegisterActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="127dp"
android:layout_marginTop="50dp" />
<EditText
android:id="@+id/edittext_username"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:hint="UserName"
android:paddingLeft="2dp" />
<EditText
android:id="@+id/edittext_password"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:hint="@string/password"
android:inputType="textPassword" />
android:hint="@string/password"/>
<EditText
android:id="@+id/edittext_cnf_password"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:hint="@string/password"
android:inputType="textPassword" />
android:hint="@string/confirm_password"/>
<Button
android:id="@+id/button_register"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#04ea00"
android:text="Daftar"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Sudah Punya Akun?"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textview_login"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:text="@string/login"
android:textColor="#04ea00"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<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"
android:background="@color/colorPrimaryDark"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="RegisterActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="127dp"
android:layout_marginTop="50dp" />
<EditText
android:id="@+id/edittext_username"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:hint="UserName"
android:paddingLeft="2dp" />
<EditText
android:id="@+id/edittext_password"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:hint="@string/password"
android:inputType="textPassword" />
android:hint="@string/password"/>
<EditText
android:id="@+id/edittext_cnf_password"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#ffffff"
android:hint="@string/password"
android:inputType="textPassword" />
android:hint="@string/confirm_password"/>
<Button
android:id="@+id/button_register"
android:layout_width="206dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#04ea00"
android:text="Daftar"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Sudah Punya Akun?"
android:textColor="#ffffff" />
<TextView
android:id="@+id/textview_login"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:text="@string/login"
android:textColor="#04ea00"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
7.Membuat ListView.Java
7.1 Source Code ListView.Java
package com.example.kelvinn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
protected ListView lv;
protected ListAdapter adapter;
SimpleAdapter Adapter;
HashMap<String, String> map;
ArrayList<HashMap<String, String>> mylist;
String[] Pil;
String[] Ltn;
String[] Gbr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
Pil = new String[] {"Samsung", "Apple", "Huawei", "Vivo", "Xiaomi"};
Ltn = new String[] {"", "", "", "", ""};
Gbr = new String[] {Integer.toString(R.drawable.samsung),
Integer.toString(R.drawable.apple),
Integer.toString(R.drawable.huawei),
Integer.toString(R.drawable.vivo),
Integer.toString(R.drawable.xiaomi) };
mylist = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < Pil.length; i++){
map = new HashMap<String, String>();
map.put("list", Pil[i]);
map.put("latin", Ltn[i]);
map.put("gbr", Gbr[i]);
mylist.add(map);
}
Adapter = new SimpleAdapter(this, mylist, R.layout.layout_isi_lv,
new String[] {"list", "latin", "gbr"}, new int[] {R.id.tv_nama, R.id.tv_ltn, R.id.imV});
lv.setAdapter(Adapter);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
protected ListView lv;
protected ListAdapter adapter;
SimpleAdapter Adapter;
HashMap<String, String> map;
ArrayList<HashMap<String, String>> mylist;
String[] Pil;
String[] Ltn;
String[] Gbr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
Pil = new String[] {"Samsung", "Apple", "Huawei", "Vivo", "Xiaomi"};
Ltn = new String[] {"", "", "", "", ""};
Gbr = new String[] {Integer.toString(R.drawable.samsung),
Integer.toString(R.drawable.apple),
Integer.toString(R.drawable.huawei),
Integer.toString(R.drawable.vivo),
Integer.toString(R.drawable.xiaomi) };
mylist = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < Pil.length; i++){
map = new HashMap<String, String>();
map.put("list", Pil[i]);
map.put("latin", Ltn[i]);
map.put("gbr", Gbr[i]);
mylist.add(map);
}
Adapter = new SimpleAdapter(this, mylist, R.layout.layout_isi_lv,
new String[] {"list", "latin", "gbr"}, new int[] {R.id.tv_nama, R.id.tv_ltn, R.id.imV});
lv.setAdapter(Adapter);
}
}
7.2 Membuat Layout Dari Listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
8. Membuat Layout_lv.XML
8.1 Source Code Layout_lv.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/tv_nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imV"
android:text="TextView"
android:textColor="#F80606"
android:textSize="10pt"
/>
<TextView
android:id="@+id/tv_ltn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_nama"
android:layout_below="@+id/tv_nama"
android:text="TextView"
android:textColor="#02808f"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/tv_nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imV"
android:text="TextView"
android:textColor="#F80606"
android:textSize="10pt"
/>
<TextView
android:id="@+id/tv_ltn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_nama"
android:layout_below="@+id/tv_nama"
android:text="TextView"
android:textColor="#02808f"
/>
</RelativeLayout>
maka sudah selesai untuk pembuatan aplikasi Daftar Hp Menggunakan Sqlite. Sekarang kita akan masuk ke tahap uji coba atau kita coba Run Aplikasinya.
Yang Pertama Kita masuk Ke form Login, sebelumnya kita harus daftar terlebih dahulu.
Dan dibawah ini adalah form untuk Daftar Atau register untuk masuk ke aplikasi
Setelah sudah daftar kita akan login ke aplikasinya. dan dibawah ini adalah tampilan setelah login
Berikut Adalah Video Hasil Running dari aplikasinya..
Sekian penjelasan dari aplikasi daftar handphone menggunakan sqlite. sekiranya ada pertanyaan silakan isi di kolom komentar.. Terimakasih GBU... :)
Tag :// UAS
Aplikasi Daftar Handphone Dengan ListView Dan Intent Menggunakan Android Studio
Aplikasi Daftar Handphone Dengan ListView Dan Intent Menggunakan Android Studio Aplikasi ini saya buat menggunakan Android Studio. saya akan memberika penjelasan tentang Aplikasi Daftar Handphone Dengan ListView Dan Intent Menggunakan Android Studio dalam rangka memenuhi Tugas Ujian Tengah Semester (UTS).
Judul Project Dan Penjelasan
aplikasi ini adalah aplikasi untuk melihat daftar handphone dan spesifikasi HP, yang menggunkan listview dan intent.
Tampilan Aplikasinya
Tampilan Aplikasi |
langkah - langkah membuat project baru di android studio
- Buka Aplikasi Android Studio
- Buat Project baru dengan memilih template Empty kemudian klik Next
Pemilihan Template |
3. Masukan Nama, Tentukan Lokasi Penyimpanan, Lalu Pilih Minimum API Level. kemudian Finish
Menentukan Nama |
4. Membuat Login.XML
Login XML |
Source Code Login.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-a
uto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:background="@drawable/log1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#0091EA"> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="?android:attr/selectableItemBackground" android:onClick="loginMasuk" android:text="LOGIN" android:textColor="#FFF" android:textStyle="normal|bold" /> /> </LinearLayout> <EditText android:id="@+id/edittext_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/edittext_password" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_marginStart="3dp" android:layout_marginLeft="3dp" android:layout_marginBottom="9dp" android:fontFamily="monospace" android:hint="Username" android:inputType="textCapWords" android:textSize="30sp" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginStart="0dp" android:layout_marginLeft="0dp" android:layout_marginTop="18dp" android:background="#0091EA" android:text="KELVIN CELLULER" android:textAlignment="center" android:textColor="#fff" android:textSize="36sp" android:textStyle="normal|bold" /> <EditText android:id="@+id/edittext_password" style="@style/AppTheme" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginStart="-1dp" android:layout_marginLeft="-1dp" android:fontFamily="monospace" android:hint="Password" android:inputType="textPassword" android:textSize="30sp" /> <ImageView android:id="@+id/imageView" android:layout_width="140dp" android:layout_height="119dp" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginStart="106dp" android:layout_marginLeft="106dp" android:layout_marginTop="86dp" android:src="@drawable/k" tools:srcCompat="@drawable/k" /> </RelativeLayout>
5. Membuat LoginMain.Java
Login Java |
Source Code LoginMain.Java
package com.example.cellulervin; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText editText1 ,editText2; String text1 ,text2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().setTitle (" Form Login"); getSupportActionBar().setSubtitle (" KELVIN GUNAWAN"); } public void loginMasuk(View view) { editText1 = (EditText)findViewById(R.id.edittext_username); editText2 = (EditText) findViewById(R.id.edittext_password); text1 = editText1.getText().toString(); text2 = editText2.getText().toString(); if ((text1.contains("Kelvin"))&&((text2.contains("kelvin123")))) { Toast.makeText(this, "Login Sukses", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this, KelvinCell.class); startActivity(intent); } else if ((text1.matches("")||text2.matches(""))) /* Atau jika input text 1 dan text 2 kosong */ { //Maka akan menampilkan pesan text toast Toast.makeText(this, "Isikan Username dan Password", Toast.LENGTH_SHORT).show(); } else { //jika kedua kondisi diatas tidak memenuhi Toast.makeText(this, "Login Gagal /Username Password Salah", Toast.LENGTH_SHORT).show(); } } }
Tampilan Dari Form Login
Tampilan Form Login |
ListView Java |
Source Code Kelvincell.java
package com.example.cellulervin; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class KelvinCell<position> extends AppCompatActivity { ListView LV; String[] BrandHp = {"SAMSUNG", "APPLE", "ASUS", "OPPO", "VIVO", "XIAOMI", "HUAWEI", "REALME", "ADVAN", "ONEPLUS"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_kelvin_cell); LV = (ListView) findViewById(R.id.listView1); ArrayAdapter adapsi = new ArrayAdapter(this, android.R.layout.simple_list_item_1, BrandHp); LV.setAdapter(adapsi); LV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int itemKe, long arg3) { // TODO Auto-generated method stub String itemText = (String) LV.getItemAtPosition(itemKe); Toast.makeText(getBaseContext(), "Anda Meng Klik " + itemText, Toast.LENGTH_LONG).show(); if (itemText.equals("SAMSUNG")) { Intent I = new Intent(arg0.getContext(), Samsung.class); startActivityForResult(I, 0); } else if (itemText.equals("APPLE")) { Intent Pop = new Intent(arg0.getContext(), Apple.class); startActivityForResult(Pop, 0); } else if (itemText.equals("ASUS")) { Intent Pop = new Intent(arg0.getContext(), Asus.class); startActivityForResult(Pop, 0); } } }); } }
7. Membuat Listview XML
Listview XML |
Source Code XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".KelvinCell" > <ListView android:id="@+id/listView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/back2"> android:padding="4dp"> </ListView> </RelativeLayout>
8. Membuat ListHandphone Samsung.java
ListView Dari Samsung |
Source Code Samsung.java
package com.example.cellulervin; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class Samsung extends AppCompatActivity { ListView LV; String[] BrandHp = {"SAMSUNG A10", "SAMSUNG A20", "SAMSUNG A30", "SAMSUNG A50", "SAMSUNG A70", "SAMSUNG A80", "SAMSUNG S7", "SAMSUNG S8", "SAMSUNG S9", "SAMSUNG S10"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_kelvin_cell); LV = (ListView) findViewById(R.id.listView1); ArrayAdapter adapsi = new ArrayAdapter(this, android.R.layout.simple_list_item_1, BrandHp); LV.setAdapter(adapsi); LV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int itemKe, long arg3) { // TODO Auto-generated method stub String itemText = (String) LV.getItemAtPosition(itemKe); Toast.makeText(getBaseContext(), "Anda Meng Klik " + itemText, Toast.LENGTH_LONG).show(); if (itemText.equals("SAMSUNG A10")) { Intent I = new Intent(arg0.getContext(), Sa10.class); startActivityForResult(I, 0); } else if (itemText.equals("SAMSUNG A20")) { Intent Pop = new Intent(arg0.getContext(), Sa20.class); startActivityForResult(Pop, 0); } else if (itemText.equals("SAMSUNG A30")) { Intent Pop = new Intent(arg0.getContext(), Apple.class); startActivityForResult(Pop, 0); } } }); } }
9. Membuat List Spesifikasi
ListView Spesifikasi |
Source Code Sa20.XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:background="@drawable/back3" android:orientation="vertical" tools:context="Sa20"> <TextView android:id="@+id/getharga" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="5dp" android:text="Harga:" android:textColor="#0629F3" android:textColorHighlight="#F2F2F2" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/getos" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="32dp" android:text="OS:" android:textColor="#092CF0" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/getchipset" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="58dp" android:text="Chipset:" android:textColor="#0D2FEE" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/getgpu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="82dp" android:text="GPU:" android:textColor="#0A2CE9" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/getcpu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="105dp" android:text="CPU:" android:textColor="#0C2EEE" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/getmemory" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="130dp" android:text="MEMORY:" android:textColor="#0D30E9" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/getbattery" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="4dp" android:layout_marginTop="154dp" android:text="BATTERY:" android:textColor="#1031E9" android:textSize="15sp" android:textStyle="bold" /> <ImageView android:id="@+id/imageView2" android:layout_width="295dp" android:layout_height="337dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="30dp" android:layout_marginTop="273dp" android:src="@drawable/a20" android:textSize="15sp" android:textStyle="bold" />/> </RelativeLayout>
10. Membuat Java Class dari Sa20
Sa20Java |
Source Code Dari Sa20.java
package com.example.cellulervin; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class Sa20 extends AppCompatActivity { private TextView harga, OS, Chipset, GPU, CPU, MEMORY, BATTERY; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sa20); harga = findViewById(R.id.getharga); OS = findViewById(R.id.getos); Chipset = findViewById(R.id.getchipset); GPU = findViewById(R.id.getgpu); CPU = findViewById(R.id.getcpu); MEMORY = findViewById(R.id.getmemory); BATTERY = findViewById(R.id.getbattery); harga.setText("Harga: Rp. 2.015.000"); OS.setText("OS: Android 9.0 (Pie); One UI"); Chipset.setText("Chipset: Exynos 7884 (14 nm)"); GPU.setText("GPU: Mali-G71 MP2"); CPU.setText("CPU: Octa-core (2x1.6 GHz Cortex-A73 & 6x1.35)"); MEMORY.setText("Memory: Storage 32GB , RAM 3GB"); BATTERY.setText("Battery: Non-removable Li-Po 4000 mAh"); } }
maka sudah selesai untuk pembuatan aplikasi listview Handphone ini. Sekarang kita akan masuk ke tahap uji coba atau kita coba Run Aplikasinya.
yang pertama kita akan masuk ke form login
Form Login |
Tampilan Awal |
List Handphone Samsung |
Spesifikasi |
Aplikasi Daftar Handphone Dengan ListView Dan Intent Menggunakan Android Studio
Rabu, 23 Oktober 2019
Posted by kelvin
Tag :// UTS