Save Edittext and Datepicker in Shared Preferences

Asked

Viewed 359 times

2

I’m trying to save an edittext and a datepicker but they save both with a dice of the two. The date Picker when clicking Editext2 it opens the calendar where you click and it selects the date for edittext2.

I just need to know how to save them both by clicking the Activity button.

basically i want to save the oil mileage in edittext1, and the date that was made in edittext2. the boot will save for me!

OIL CLASS

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;


public class oleo extends Activity implements View.OnClickListener {



    private Button buttonSalvarOleo;
    private SharedPreferences savednotes;
    private EditText editTextOleo;
    private EditText editTextData;
    Button buttonOleoRec;


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oleo);



        buttonOleoRec = (Button)findViewById(R.id.buttonOleoRec);
        buttonOleoRec.setOnClickListener(this);

        buttonSalvarOleo = (Button) findViewById(R.id.buttonSalvarOleo);
         editTextOleo = (EditText) findViewById(R.id.editTextOleo);
         savednotes = getSharedPreferences("notes",MODE_PRIVATE);

        editTextData = (EditText) findViewById(R.id.editTextData);


        editTextOleo.setText(savednotes.getString("tag", "00.000")); //adicionar essa linha


        buttonSalvarOleo.setOnClickListener(saveButtonListener);
    }

    //AQUI EH OQUE CHAMA O CALENDARIO
    public void onStart(){
        super.onStart();
        EditText editTextData = (EditText)findViewById(R.id.editTextData);
        editTextData.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    DateDialog dialog= new DateDialog(v);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    dialog.show(ft, "DatePicker");
                }
            }

        });

    }

    private void makeTag(String tag){
        String or = savednotes.getString(tag, null);
        SharedPreferences.Editor preferencesEditor = savednotes.edit();
        preferencesEditor.putString("tag",tag); //change this line to this
        preferencesEditor.commit();
    }

    public View.OnClickListener saveButtonListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(editTextOleo.getText().length()>0){
                makeTag(editTextOleo.getText().toString());

                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editTextOleo.getWindowToken(),0);

            }


        }
    };

    private void buttonOleoRecClick(){
        //NOME DO LAYOUT Q ESTA NO MANIFEST
        startActivity(new Intent("layout.activity_oleorecomendado"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //fazer pros outros botoes aki
            case R.id.buttonOleoRec:
                buttonOleoRecClick();
                break;
        }
    }
}

DATEDIALOG CLASS

import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;

/**
 * Created by Bruno Marna on 9/19/2015.
 */
public class DateDialog  extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    EditText editTextData;
    public DateDialog(View view){
        editTextData=(EditText)view;
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar c=Calendar.getInstance();
        int year =c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int day=c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this,year,month,day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        String date=day+"/"+(month+1)+"/"+year;
        editTextData.setText(date);

    }

}

ACTIVITY

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/editTextOleo"
        android:layout_alignBottom="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignEnd="@+id/textView2" />

<Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Salvar"
        android:id="@+id/buttonSalvarOleo"
        android:layout_weight="1.12"
        android:layout_gravity="right|center_vertical"
        android:background="#444"
        android:textColor="#FFF"
        android:textStyle="bold"
        android:layout_marginTop="33dp"
        android:layout_below="@+id/editTextData"
        android:layout_alignRight="@+id/textView3"
        android:layout_alignEnd="@+id/textView3" />

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editTextData"
        android:hint="dia/mês/ano"
        android:layout_alignBottom="@+id/textView10"
        android:layout_alignLeft="@+id/editTextOleo"
        android:layout_alignStart="@+id/editTextOleo" />

1 answer

1

Try The Next:

//SALVA OS DADOS:
    //TAG: é o ID para resgatar o valor
    //VALUE: o valor a ser armazenado
    private void makeTag(final String tag, final String value){
        SharedPreferences.Editor preferencesEditor = savednotes.edit();
        /// passamos a TAG, a qual vamos usar para resgatar
        preferencesEditor.putString(tag, value);
        preferencesEditor.commit();
    }
//RESGATA OS DADOS
    //TAG: é o ID para resgatar o valor
    private void getTag(final String tag){
        String value = savednotes.getString(tag, null);
        return value;
    }

To make it easier, I suggest you create cosntantes with the name of the tags:

public final String edittext1TAG = "TAG1";
public final String edittext2TAG = "TAG2";

When you save :

makeTag(edittext1TAG, editTextOleo.getText().toString());

Greetings,

  • Nothing yet :( remains the same... sorry but I’m new to programming and I made these changes but it’s the same before

  • Show me how you did it! no need to apologize! The purpose of this site is to help ! Post your changes so we can help you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.