简体   繁体   中英

Android Edittext Putting edittext values into array and Sharedpreferences

I am very newbie in software. I'm making a mobile application. in the fitness area. I have arrays. when I press the forward and back key. The registered arrays of string and int type are displayed in order. I did these, no problem. Here is what I want to do. there is an edittext on the page. users will enter weight in edittext and that weight will be saved. When the application is opened again, when the forward button is pressed, the last entered and saved data in edittext will come in order. how can I do that.I searched for sharedpreferences sqlite. but i couldn't

I want to add the data entered in edittext to the array, and the data saved in edittext will be displayed at each step when the forward button is pressed sequentially.

public class MainActivity extends AppCompatActivity {
EditText edittxt_agirlik;
Button buton_save, goster_buton, button_ileri;
TextView judul,isi ,txtkaydedilen;
ImageView gambar;
int halaman =0;


private String veri_string;



String[] judul_artikel = new String[]{
    "satu",
        "dua",
        "tiga",
        "empat",
        "lima"

};


String[] isi_artikel = new String[]{
        "satu 6 54654654654",
        "dua 3151515",
        "tiga 6484616546",
        "empat 514616514654",
        "lima 64684684684"

};

int[] gambar_artikel = new int[]{
            R.drawable.barbellrow,
            R.drawable.benchpress,
            R.drawable.militarypress,
            R.drawable.pullover,
            R.drawable.pullup,

};



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

    judul = findViewById(R.id.judul);
    isi = findViewById(R.id.isi);
    gambar = findViewById(R.id.gambar);
    edittxt_agirlik = findViewById(R.id.edittxt_agirlik);
    buton_save = findViewById(R.id.buton_save);
    button_ileri = findViewById(R.id.button_ileri);


    SharedPreferences settings = getSharedPreferences("preference_name",MODE_PRIVATE);
    veri_string = settings.getString("save_kg",null);
    edittxt_agirlik.setText(veri_string);


    judul.setText(judul_artikel[halaman]);
    isi.setText(isi_artikel[halaman]);
    gambar.setImageResource(gambar_artikel[halaman]);



    buton_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        SharedPreferences settings = getSharedPreferences("preference_name",MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("save_kg",edittxt_agirlik.getText().toString());
        editor.commit();


        }
    });



}



public void next(View view){


    if (halaman<4){


        halaman++;

        judul.setText(judul_artikel[halaman]);
        isi.setText(isi_artikel[halaman]);
        gambar.setImageResource(gambar_artikel[halaman]);


    }



}

public void prev(View view){

    if (halaman>0){

    halaman--;
    judul.setText(judul_artikel[halaman]);
    isi.setText(isi_artikel[halaman]);
    gambar.setImageResource(gambar_artikel[halaman]);



    }

}

}

I would recommend looking at the Android Developers guide to SharedPreferences here .

There are two ways of saving your data from EditText

  1. add Text change listener on editText by doing this:
      edittxt_agirlik.addTextChangedListener(new TextWatcher() { 
      @Override 
        public void onTextChanged(CharSequence s, int start, int before, int count){ 
          SharedPreferences settings = getSharedPreferencess("preference_name",MODE_PRIVATE);
          SharedPreferences.Editor editor = settings.edit();
          editor.putString("save_kg",edittxt_agirlik.getText().toString());
          editor.commit();
      }
      
      @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, int afterr) {
            // TODO Auto-generated method stub
        }
        
       @Override 
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });
  1. you can save data before closing the app by implementing the AppCombat override methods like onBackPressed(), onStop(), onDestroy() methods. These methods will be called before you press back button, you kill the app from menu.

For example:

    @Override void onBackPressed(){

        SharedPreferences settings = getSharedPreferencess("preference_name",MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("save_kg",edittxt_agirlik.getText().toString());
        editor.commit();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM