0

I've created a dynamic ListView, but the data in my ListView are deleted on every boot I have to do for it? Could you write sample applications?

I have an earlier opening on the first boot record program how data will bring? I know I need to use SharedPreferences, but how do I use, my experiment failed.

private SharedPreferences ayarla;
private String AYAR_ADI="ayarlist";
private SharedPreferences.Editor editor;
private ArrayList<String> dersler = new ArrayList<String>();

protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); 
adapter  = new ArrayAdapter<String>(this,R.layout.customlistview,dersler);
ayarla = getApplicationContext().getSharedPreferences(AYAR_ADI, Context.MODE_PRIVATE);
Button btnDersEkle = (Button)findViewById(R.id.btnDersEkle);

btnDersEkle.setOnClickListener(new OnClickListener() {      
    @Override
    public void onClick(View v) {
        if(dersler.size()<12) {         
            lV.clearTextFilter();
            adapter.notifyDataSetChanged();                 
            alert.setView(et());    
            alert.show();
        }       
    }
});
lV = (ListView)findViewById(R.id.lV);
lV.setAdapter(adapter);

alert = new AlertDialog.Builder(this);
alert.setTitle("Ders Giriniz...");
alert.setMessage("En fazla 12 ders ekleyebilirsiniz.");
alert.setPositiveButton("Ekle", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

        ayarla = getApplicationContext().getSharedPreferences(AYAR_ADI, Context.MODE_PRIVATE);
        editor = ayarla.edit();
        if((null!=editText.getText().toString()&&editText.getText().toString().length()>0)){  
            dersler.add(editText.getText().toString());
        //  editor.putString("ayarlist", String.valueOf(editText.getText().toString()));
            alert.setView(et());    
            alert.show();           

            editor.putString("ayarlist",editText.getText().toString());             
            editor.commit();

        }
    }
});

}

2 Answers2

0

Because you are not saving your data. To save your listview, you can either use a database or use SharedPreferences. You can see detailed usage of SharedPreferences here:

http://developer.android.com/training/basics/data-storage/shared-preferences.html

and the usage of database here:

http://developer.android.com/training/basics/data-storage/databases.html;

Hope this helps

yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • Thank you for your answer first. but the problem with the data to be presented at the opening. I share the relevant parts of my code below. I already use SharedPreferences – user3434736 Mar 21 '14 at 21:19
0

Hye, if you still find answer so i have one, even after long long searching i did find this working solution.! I made a function of storing info into Shared-Preferences using array-list.

Sending Activity:

public void Write() {
    if (TextUtils.isEmpty(etDisp.getText().toString())) {
        return;
    } else {

        // ====== ARRAY-LIST =======
        sendinglist.add(etDisp.getText().toString()); // sendinglist is an  ArrayList<String>
        Editor editor = sp.edit(); //SharedPreferences sp;
        try {
            editor.putString("svalue",
                    ObjectSerializer.serialize(sendinglist));
        } catch (IOException e) {
        }
        editor.commit();

    }
}

To retrieve from array list.!

try {
        receivinglist = (ArrayList<String>) ObjectSerializer.deserialize(sp
                .getString("svalue", "N/A")); // receivinglist is an  ArrayList<String>
    } catch (IOException e) {
        e.printStackTrace();
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_expandable_list_item_1, receivinglist);
    setListAdapter(adapter);

I'm currently using this approach in my Project.

while ObjectSerializer.java is a class which involves logic code for this working You can get it from Here

mfaisalhyder
  • 2,250
  • 3
  • 28
  • 38