0

This code is only for testing, how to save existing client data when the save button is clicked and can still be used to view, update and delete using shared preferences so that when the application exits the data is not lost, here's the code:

package com.test.data;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.List;
import android.view.Gravity;

class Client {
    private String clientID;
    private String clientName;
    private String clientAddress;
    private String clientEmail;
    private String clientPhone;

    public Client(String clientID, String clientName, String clientAddres, String clientEmail, String clientPhone) {
        this.clientID = clientID;
        this.clientName = clientName;
        this.clientAddress = clientAddres;
        this.clientEmail = clientEmail;
        this.clientPhone = clientPhone;
    }

    public String getclientID() {
        return clientID;
    }
    
    public void setclientID(String clientID) {
        this.clientID = clientID;
    }
    
    public String getclientName() {
        return clientName;
    }

    public void setclieantName(String clientName) {
        this.clientName = clientName;
    }

    public String getclientAddress() {
        return clientAddress;
    }

    public void setclientAddress(String clientAddress) {
        this.clientAddress = clientAddress;
    }

    public String getclientEmail() {
        return getclientEmail();
    }

    public void setclientEmail(String clientEmail) {
        this.clientEmail = clientEmail;
    }

    public String getclientPhone() {
        return clientPhone;
    }

    public void setclientPhone(String clientPhone) {
        this.clientPhone = clientPhone;
    }
}


public class MainActivity extends AppCompatActivity {

    private EditText etclientID, etclientName, etclientAddress, etclientEmail, etclientPhone;
    private Button btnSave, btnShow, btnUpdate, btnDelete;
    
    private List<Client> listClient = new ArrayList<>();

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

        etclientID = findViewById(R.id.et_id_client);
        etclientName = findViewById(R.id.et_nama_client);
        etclientAddress = findViewById(R.id.et_waktu_pendaftaran);
        etclientEmail = findViewById(R.id.et_jenis_perkara);
        etclientPhone = findViewById(R.id.et_status_perkara);

        btnSave = findViewById(R.id.btn_simpan);
        btnShow = findViewById(R.id.btn_lihat);
        btnUpdate = findViewById(R.id.btn_update);
        btnDelete = findViewById(R.id.btn_hapus);

        btnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveClient();
                }
            });
        
        
        btnShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showClient();
                }
            });

        btnUpdate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateClient();
                }
            });

        btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteClient();
                }
            });
    }

    private void saveClient() {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout, null);
        TextView text = layout.findViewById(R.id.text);
        
        String clientID = etclientID.getText().toString();
        String clientName = etclientName.getText().toString();
        String clientAddress = etclientAddress.getText().toString();
        String clientEmail = etclientEmail.getText().toString();
        String clientPhone = etclientPhone.getText().toString();

        boolean isIdExists = false;
        for (Client client : listClient) {
            if (client.getclientID().equals(clientID)) {
                isIdExists = true;
                break;
            }
        }

        if (isIdExists) {
            text.setText("Client ID is already in use");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        } else {
            listClient.add(new Client(clientID, clientName, clientAddress, clientEmail, clientPhone));
            text.setText("Data saved successfully");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
    }
}
    
    private void showClient() {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout, null);
        TextView text = layout.findViewById(R.id.text);
        
        String clientID = etclientID.getText().toString();
        boolean found = false;
        for (Client client : listClient) {
            if (client.getclientID().equals(clientID)) {
                text.setText("Client ID : " + client.getclientID() + "\nClient Name : " + client.getclientName() + "\nClient Address : " + client.getclientAddress() + "\nClient Email : " + client.getclientAddress() + "\nClient Phone : " + client.getclientPhone());
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.TOP, 0,30);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
                found = true;
                break;
            }
        }
        if (!found) {
            text.setText("Data with id " + clientID + " not found");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        }
    }

    private void updateClient() {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout, null);
        TextView text = layout.findViewById(R.id.text);
        
        String clientID = etclientID.getText().toString();
        String clientName = etclientName.getText().toString();
        String clientAddress = etclientAddress.getText().toString();
        String clientEmail = etclientEmail.getText().toString();
        String clientPhone = etclientPhone.getText().toString();

        boolean isUpdated = false;

        for (Client client : listClient) {
            if (client.getclientID().equals(clientID)) {
                client.setclieantName(clientName);
                client.setclientAddress(clientAddress);
                client.setclientEmail(clientEmail);
                client.setclientPhone(clientPhone);
                isUpdated = true;
                break;
            }
        }

        if (isUpdated) {
            text.setText("Data successfully updated");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        } else {
            text.setText("Data not found");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        }
    }

    private void deleteClient() {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout, null);
        TextView text = layout.findViewById(R.id.text);
        
        String clientID = etclientID.getText().toString();

        int index = -1;
        for (int i = 0; i < listClient.size(); i++) {
            if (listClient.get(i).getclientID().equals(clientID)) {
                index = i;
                break;
            }
        }

        if (index != -1) {
            listClient.remove(index);
            text.setText("Data berhasil dihapus");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        } else {
            text.setText("Data deleted successfully");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.TOP, 0, 30);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        }
    }
}

I want the data not to be lost when I exit the application, when I enter the application again the data can still be used per id, all functions saveclient, showclient, updateclient, deleteclient can still be used, maybe someone can help.

lee07
  • 1
  • 2
    It would be better for you to adopt [SQLite](https://developer.android.com/training/data-storage/sqlite) for saving objects. But if you insist using SharedPreferences, you can [create a custom SharedPreferences helper class](https://stackoverflow.com/questions/46763570/custom-sharedpreferences-class) and [serialize your object using Gson](https://stackoverflow.com/questions/5816695/android-sharedpreferences-with-serializable-object) before saving. – Enowneb Feb 13 '23 at 03:47

0 Answers0