[Android] I have a question about parcellable.

Asked 2 years ago, Updated 2 years ago, 26 views

I'm making a list of words that I use often through parcelable.

I even implemented the list, created it using Edittext, and even deleted the values in the list. However,

"When I turn off the app, the list evaporates. "

So I tried to use shared preference, but the parallel is not compatible, so there are posts in stack overflow. Is it difficult to save settings without using DB? If possible, please tell me how! ㅠ<

MacroActivity.java

package com.example.junseo.test03;

//List representation activity

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;

/**
 * * Created by Junseo on 2017-06-30.
 */

public class MacroActivity extends AppCompatActivity {
    ListView listview;

    ArrayAdapter<String> adapter;
    TextView tv;

    int Addition = 1;

    ArrayList<macro> storage = new ArrayList<>();
    ArrayList<String> title = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_macro);
        setTitle ("My Commercial Sphere");

        init();
    }
    void init(){
        listview = (ListView)findViewById(R.id.listview);
        tv = (TextView)findViewById(R.id.tv);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, title);

        listview.setAdapter(adapter);

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlertDialog.Builder dlg = new AlertDialog.Builder(MacroActivity.this);
                final int position = i;
                dlg.setTitle ("delete")
                        .setMessage ("Do you want to delete it?")
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                title.remove(position);
                                storage.remove(position);
                                adapter.notifyDataSetChanged();
                                tv.setText ("Commercial phrase list" + title.size() + "dog"));
                            }
                        }).show();
                return true;
            }
        });

        //If you clicked List.
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Intent intent = new Intent(MainActivity.this, Main3Activity.class);

              //  //  intent.putExtra("macro", storage.get(i));

              //  //  startActivity(intent);
            }
        });
    }
    //Go to Macro Correction Activity.
    public void onClick(View v){
        Intent intent = new Intent(this, MacroupActivity.class);

        startActivityForResult(intent, Addition);
    }

    @Override
    //After modifying the commercial district, display the number of commercial districts and import data.
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == Addition){
            if(resultCode == RESULT_OK){
                Macro = data.getParcelableExtra("macro"); // Gets the data temporarily stored in macro.java.
                Title.add(r.getName()); // Get the contents of the commercial tool.
                storage.add(r);
                adapter.notifyDataSetChanged();
                tv.setText ("Commercial phrase list" + title.size() + "dog"));
            }
        }
    }
}

MacroupActivity.java

package com.example.junseo.test03;
// Edit activity for putting a value in the list.
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;



public class MacroupActivity extends AppCompatActivity {

    EditText etName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_macroup);
        setTitle ("My Commercial Sphere");

        init();
    }

    void init() {
        etName = (EditText) findViewById(R.id.etname);
    }

    public void onClick(View v) {
        if (v.getId() == R.id.btnCancel) {
            finish();
        } } else if (v.getId() == R.id.btnAdd) {
            Intent intent = new Intent();

            String name = etName.getText().toString(); // Put the input value into the name value.

            Macro temp = new macro(name); // name value is added to the temp value according to the format implemented in macro.java.

            int.putExtra ("macro", temp); // temporarily stored in macro.java for transmission to MacroActivity.
            setResult(RESULT_OK, intent);
            finish();

        }
    }
}

macro.java (Parcelable)

package com.example.junseo.test03;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * * Created by Junseo on 2017-07-03.
 */

public class macro implements Parcelable {

    private String name;

    public macro(String name){
        this.name = name;

    }
    protected macro(Parcel in) {
        name = in.readString();
    }

    public static final Creator<macro> CREATOR = new Creator<macro>() {
        @Override
        public macro createFromParcel(Parcel in) {
            return new macro(in);
        }
        @Override
        public macro[] newArray(int size) {
            return new macro[size];
        }
    };
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

android

2022-09-22 19:34

1 Answers

Parcelable is an Android-specific serialization technique that replaces Java's serializable annotation, which is used to store object data in Intent and transfer it to other components.

It has nothing to do with the perpetuation of the data.

To perpetuate data without using DB, you can use Shared Preferences as you mentioned.

By default, Shared Preferences on Android cannot hold data except for some Primary Types.

To do this, you can wrap the data you want to store in a model class, make it a JSON-style String using an Object-JSON mapper library like GSON, and keep it in a Shared Preference.


2022-09-22 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.