Soru & Cevap

preference ile ArrayList Kaydetme ...

20.05.2017 - 02:36
public class MainActivity extends ListActivity {
    private static final String[] items={" Harcama ve Kazanç Tablosu"};
    public static final int MENU_ADD = Menu.FIRST+1;


    public static final int MENU_REMOVE = Menu.FIRST+4 ;
    ArrayList<String> words=null;


    @Override
    protected void onPause() {

        super.onPause();
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 

        initadapter();
        registerForContextMenu(getListView());


    }

    private void initadapter() {
        words=new ArrayList<String>();
        for (String s : items) {
            words.add(s);


        }

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, words));


    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        menu.add(Menu.NONE, MENU_ADD, Menu.NONE, "Ekle");
        menu.add(Menu.NONE, MENU_REMOVE, Menu.NONE, "Sil");


        super.onCreateContextMenu(menu, v, menuInfo);
    }

    public boolean onContextItemSelected(MenuItem item) {
       AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        ArrayAdapter<String> adapter=(ArrayAdapter<String>)getListAdapter();
        {
        }
        switch (item.getItemId()) {
            case MENU_REMOVE:
                adapter.remove(words.get(info.position));

            {
                }
            case MENU_ADD:
                add();
                return(true);
        }
        return(super.onContextItemSelected(item));
    }






    private void add() {

        final View addView=getLayoutInflater().inflate(R.layout.activity_main, null);
        new AlertDialog.Builder(this)
                .setTitle("Gider veya gelir ekle.")
                .setView(addView)
                .setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                    @SuppressWarnings("unchecked")
                    public void onClick(DialogInterface dialog,
                                        int whichButton) {
                        ArrayAdapter<String> adapter=(ArrayAdapter<String>)getListAdapter();
                        EditText yazı=(EditText)addView.findViewById(R.id.title);
                        adapter.add(yazı.getText().toString());
                    }
                })
                .setNegativeButton("Geri", null)
                .show();

    }

    }

Merhaba arkadaşlar.Gündelik hayatta gider ,gelir hesabımı tutmak için kendimce ufak bir uygulama yapıyorum.Fakat bir sorunla karşılaştım.İstediğim de programı kapattıp açtığımda "items" lerin kaybolması.

onPause () kısmında preference nasıl ekleyip ardından tekrar bunları okuyabilirim ?

Eğer .başka bir yol ile de oluyorsa cevaplarınızı bekliyorum.

Yardımcı olursanız sevinirim.

 

 

241 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-7934-1439286305.jpg
tanerutku
04.06.2017 - 01:20

Merhaba,

SharedPreferences'e liste eklemek için bikaç yöntem var benim kullandığım Gson kütüphanesi ile listeyi json stringe çevirip kaydetmek, sonrasında o json stringi tekrar parse etmek.

Örnek olarak;

private void SaveListToPhone(Context context, List<Deneme> list) {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        editor.putString("SAVED_LIST", json);
        editor.commit();

    }

private void GetListFromPhone(Context context){
        String jsonPreferences;
        List<Deneme> list = new ArrayList<>();
        SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
        jsonPreferences = settings.getString("SAVED_LIST", "");
        Type type = new TypeToken<List<Deneme>>() {}.getType();
        list = gson.fromJson(jsonPreferences, type);
}