Praktikum 6 | Tugas 2 | Menu Register dan Viewlist

Disini saya akan membuat Menu Register Dan Viewlist :

Project Name : Tugas2
Buitl Target : Android 2.2
Application name : DBPratice
Package name : com.wilis.database4
Activity : main

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_height="wrap_content"
        android:id="@+id/textViewMainHeading"
        android:layout_width="wrap_content"
        android:text="Slect your choice"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
       
    </TextView>
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Register" android:id="@+id/buttonMainRegister"
        android:layout_below="@+id/textViewMainHeading"
        android:layout_centerHorizontal="true" android:layout_marginTop="70dp">
       
    </Button>
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="View List and Details"
        android:id="@+id/ButtonMainList"
        android:layout_below="@+id/textViewMainHeading"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="133dp">
       
    </Button>
</RelativeLayout>

registration.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/buttonRegisterBack"
        android:text="Back">
       
    </Button>
    <EditText android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:id="@+id/editTextRegName"
        android:layout_width="120dp"
        android:layout_below="@+id/buttonRegisterBack"
        android:layout_toRightOf="@+id/textViewRegName"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="18dp">
        <requestFocus>
           
        </requestFocus>
    </EditText>
    <TextView android:layout_height="wrap_content"
        android:id="@+id/textViewRegName"
        android:layout_width="wrap_content"
        android:text="Name"
        android:layout_alignBottom="@+id/editTextRegName"
        android:layout_toRightOf="@+id/buttonRegisterBack"
        android:layout_marginLeft="36dp"
        android:layout_marginBottom="14dp">
       
    </TextView>
    <EditText android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:layout_width="120dp"
        android:layout_below="@+id/editTextRegName"
        android:layout_alignLeft="@+id/editTextRegName"
        android:layout_marginTop="26dp"
        android:id="@+id/editTextRegAddress">
       
    </EditText>
    <TextView android:layout_height="wrap_content"
        android:id="@+id/textViewRegAddress"
        android:layout_width="wrap_content"
        android:text="Address"
        android:layout_alignTop="@+id/editTextRegAddress"
        android:layout_alignLeft="@+id/textViewRegName"
        android:layout_marginTop="14dp">
       
    </TextView>
    <EditText android:layout_height="wrap_content"
        android:inputType="phone"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/editTextRegAddress"
        android:id="@+id/editTextRegPhone"
        android:layout_width="120dp">
       
    </EditText>
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/textViewRegAddress"
        android:id="@+id/textViewRegPhone"
        android:text="Phone No.">
       
    </TextView>
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/editTextRegPhone"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:id="@+id/buttonRegister"
        android:text="Register">
       
    </Button>
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/login_icon"
        android:layout_below="@+id/buttonRegister"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:id="@+id/imageViewRegLogin">
       
    </ImageView>
   
</RelativeLayout>

details_users.xml :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/login_icon"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp"
        android:id="@+id/imageViewDetailsUserIcon">
       
    </ImageView>
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/imageViewDetailsUserIcon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:id="@+id/textViewDetailsName"
        android:text="Name">
       
    </TextView>
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/textViewDetailsName"
        android:layout_alignLeft="@+id/textViewDetailsName"
        android:layout_marginTop="28dp"
        android:id="@+id/textViewDetailsAddress"
        android:text="Address">
       
    </TextView>
    <TextView android:layout_height="wrap_content"
        android:id="@+id/textViewDetailsPhone"
         android:layout_width="wrap_content"
         android:layout_below="@+id/textViewDetailsAddress"
         android:layout_alignLeft="@+id/textViewDetailsAddress"
         android:layout_marginTop="28dp"
         android:text="Phone">
       
    </TextView>
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/buttonDetailsBack"
        android:text="Back">
       
    </Button>
    <Button android:id="@+id/buttonEditDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit Details"
        android:layout_below="@+id/textViewDetailsPhone"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp">
       
    </Button>
   
</RelativeLayout>

edit_user.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/buttonEditBack"
        android:text="Back">
       
    </Button>
    <TextView android:layout_height="wrap_content"
        android:id="@+id/textViewEditAddress"
        android:layout_width="wrap_content"
        android:text="Address"
        android:layout_alignTop="@+id/editTextEditAddress"
        android:layout_alignLeft="@+id/textViewEditName"
        android:layout_marginTop="14dp">
       
    </TextView>
    <Button android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/editTextEditPhone"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:id="@+id/buttonEdit"
        android:text="Update">
       
    </Button>
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/login_icon"
        android:layout_below="@+id/buttonEdit"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:id="@+id/imageViewEditLogin">
       
    </ImageView>
    <TextView android:id="@+id/textViewEditName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:layout_alignBottom="@+id/editTextEditName"
        android:layout_toRightOf="@+id/buttonEditBack"
        android:layout_marginBottom="14dp">
       
    </TextView>
    <EditText android:layout_width="120dp"
        android:inputType="textPersonName"
        android:layout_height="wrap_content"
        android:id="@+id/editTextEditName"
        android:layout_below="@+id/buttonEditBack"
        android:layout_alignLeft="@+id/buttonEdit">
        <requestFocus>
           
        </requestFocus>
    </EditText>
    <EditText android:layout_width="120dp"
        android:inputType="textPostalAddress"
        android:layout_height="wrap_content"
        android:id="@+id/editTextEditAddress"
        android:layout_below="@+id/editTextEditName"
        android:layout_alignLeft="@+id/editTextEditName"
        android:layout_marginTop="14dp">
       
    </EditText>
    <EditText android:layout_width="120dp"
        android:inputType="phone"
        android:layout_height="wrap_content"
        android:id="@+id/editTextEditPhone"
        android:layout_below="@+id/editTextEditAddress"
        android:layout_alignLeft="@+id/editTextEditAddress"
        android:layout_marginTop="14dp">
       
    </EditText>
    <TextView android:id="@+id/textViewEditPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone No."
        android:layout_alignTop="@+id/editTextEditPhone"
        android:layout_toRightOf="@+id/buttonEditBack"
        android:layout_marginTop="15dp">
       
    </TextView>
</RelativeLayout>
 

list_users.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/login_icon" >
    </ImageView>

    <TextView
        android:id="@+id/textViewList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/icon"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/icon"
        android:text="@+id/label"
        android:textSize="20px" />
   
</RelativeLayout>
 
Silahkan Download gambar berikut ini : 






Setelah itu kita copy gambar tersebut ke res/drawable-hdpi
Berikut ini source code pada class.

DBPracticeActivity.java :

package com.example.activity;

import com.example.db.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DBPracticeActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button regButton = (Button)findViewById(R.id.buttonMainRegister);
        Button listButton = (Button)findViewById(R.id.ButtonMainList);
        regButton.setOnClickListener(this);
        listButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch(v.getId()) {
        case R.id.buttonMainRegister :
            intent = new Intent();
            intent.setClass(getApplicationContext(), Registration.class);
            startActivity(intent);
            break;
        case R.id.ButtonMainList :
            intent = new Intent();
            intent.setClass(getApplicationContext(), ListUsers.class);
            startActivity(intent);
            break;
        }
    }
}

DetailsUsers.java :

  package com.example.activity;

import com.example.db.DatabaseHandler;
import com.example.db.R;
import com.example.db.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DetailsUsers  extends Activity implements OnClickListener{
    private static String position = null;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_users);
        Intent intent = getIntent();
        position = intent.getStringExtra("position");
        Log.d("value of position from intent", position);
        DatabaseHandler db = new DatabaseHandler(this);
        User user = db.getUser(position);
        TextView textViewDetailName = (TextView) findViewById(R.id.textViewDetailsName);
        textViewDetailName.setText(user.getName());
      
        TextView textViewDetailAddress = (TextView) findViewById(R.id.textViewDetailsAddress);
        textViewDetailAddress.setText(user.getAddress());
      
        TextView textViewDetailsPhone = (TextView) findViewById(R.id.textViewDetailsPhone);
        textViewDetailsPhone.setText(user.getPhone());
      
        Button backButton = (Button)findViewById(R.id.buttonDetailsBack);
        backButton.setOnClickListener(this);
      
        Button editButton = (Button)findViewById(R.id.buttonEditDetails);
        editButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.buttonDetailsBack) {
            Intent intent = new Intent(getApplicationContext(), ListUsers.class);
            startActivity(intent);
        }
        else if(v.getId() == R.id.buttonEditDetails) {
            Intent intent = new Intent(getApplicationContext(), EditUsers.class);
            intent.putExtra("position", position);
            startActivity(intent);
        }
    }
}

EditUsers.java :

package com.example.activity;

import com.example.db.DatabaseHandler;
import com.example.db.R;
import com.example.db.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditUsers extends Activity implements OnClickListener{
    private static String position = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_user);
        Intent intent = getIntent();
        position = intent.getStringExtra("position");

        DatabaseHandler db = new DatabaseHandler(this);
        Log.d("position", position);
        //get user for the position clicked for edit
        User user = db.getUser(position);
      
        Log.d("User Name", user.getName());
        Log.d("Address", user.getAddress());
        Log.d("Users phone", user.getPhone());
      
        //set the values in editText boxes
        EditText name = ((EditText)findViewById(R.id.editTextEditName));
        name.setText(user.getName());
        EditText address = ((EditText)findViewById(R.id.editTextEditAddress));
        address.setText(user.getAddress());
        EditText phone = ((EditText)findViewById(R.id.editTextEditPhone));
        phone.setText(user.getPhone());
      
        Button backButton = (Button)findViewById(R.id.buttonEditBack);
        backButton.setOnClickListener(this);
      
        Button updateButton = (Button)findViewById(R.id.buttonEdit);
        updateButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.buttonEditBack) {
            Intent intent = new Intent(getApplicationContext(), ListUsers.class);
            startActivity(intent);
        }
        else if(v.getId() == R.id.buttonEdit) {
            User user = new User(
                            ((EditText)findViewById(R.id.editTextEditName)).getText().toString(),
                            ((EditText)findViewById(R.id.editTextEditAddress)).getText().toString(),
                            ((EditText)findViewById(R.id.editTextEditPhone)).getText().toString()
                        );
            DatabaseHandler db = new DatabaseHandler(this);
            int updateCount = db.editUser(user, position);
            if(updateCount == 1) {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "User successfully updated",
                        Toast.LENGTH_SHORT);
                toast.show();
            }
            else{
                Toast toast = Toast.makeText(getApplicationContext(),
                        "User update failed, Try Again",
                        Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

}
 
ListUsers.java :

package com.example.activity;

import java.util.List;

import com.example.db.DatabaseHandler;
import com.example.db.R;
import com.example.db.User;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListUsers extends ListActivity{
   
    public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
         DatabaseHandler db = new DatabaseHandler(this);
        String[] users = db.getAllUsers();
        if(users!=null) {
            for(String us:users) {
                Log.d("String Array Value", us);
            }
            db.close();
        }
        // Use your own layout
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_users, R.id.textViewList, users);
        setListAdapter(adapter);
    }
   
     @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
        Log.d("On click of a item", Integer.toString(position));
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), DetailsUsers.class);
        intent.putExtra("position", Integer.toString(position + 1));  //position starts from 0, but in db row starts from 1
        startActivity(intent);
      }
}
 
Registration.java :

package com.example.activity;

import com.example.db.DatabaseHandler;
import com.example.db.R;
import com.example.db.User;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Registration extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);
        Button regButton = (Button)findViewById(R.id.buttonRegister);
        regButton.setOnClickListener(this);
        Button backButton = (Button)findViewById(R.id.buttonRegisterBack);
        backButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch(v.getId()) {
        case R.id.buttonRegister :
            register();
            break;
        case R.id.buttonRegisterBack :
            intent = new Intent();
            intent.setClass(getApplicationContext(), DBPracticeActivity.class);
            startActivity(intent);
            break;
        }  
    }

    private void register() {
        String user = ((EditText)findViewById(R.id.editTextRegName)).getText().toString();
        String address = ((EditText)findViewById(R.id.editTextRegAddress)).getText().toString();
        String phone = ((EditText)findViewById(R.id.editTextRegPhone)).getText().toString();
        DatabaseHandler db = new DatabaseHandler(this);
        int id = db.addUser(new User(user,address,phone));
        db.close();
        if(id>0) {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "User registered at id " + id,
                    Toast.LENGTH_SHORT);
            toast.show();
        }
        else {
            Toast toast = Toast.makeText(getApplicationContext(),
                    "User registration failed",
                    Toast.LENGTH_SHORT);
            toast.show();
        }
        ((EditText)findViewById(R.id.editTextRegName)).setText("");
        ((EditText)findViewById(R.id.editTextRegAddress)).setText("");
        ((EditText)findViewById(R.id.editTextRegPhone)).setText("");
    }

}
 

Hasil Running :

 
 

0 komentar:

Posting Komentar