0

i have a problem which is making me crazy. i know how to use "SharedPreferences" in android but i dont know where to put it in my project,like onCreate(),onDestroy() or somewhere else. *i want to save NoteLab.mNotes in NoteListFragment

*Note.java

package notes.com.example.alireza;
public class Note {
public UUID nId;
public Date nDate;
public String nTitle;
public String nDetails;

public Note(){
    nId = UUID.randomUUID();
    nDate = new Date();
}
public Date getnDate(){
    return nDate;
}
public UUID getnUUID(){
    return nId;
}

public String getnTitle() {
    return nTitle;
}

public void setnTitle(String nTitle) {
    this.nTitle = nTitle;
}

public String getnDetails() {
    return nDetails;
}

public void setnDetails(String nDetails) {
    this.nDetails = nDetails;
}
}

*NoteLab.java

package notes.com.example.alireza;
import android.content.Context;
import java.util.ArrayList;
import java.util.UUID;

class NoteLab {
public static ArrayList<Note> mNotes;
public static NoteLab noteLab;
public Context mAppContext;


private NoteLab(Context AppContext){
    mAppContext = AppContext;
    mNotes = new ArrayList<Note>();

}

public static NoteLab get(Context context){
    if (noteLab == null){
        noteLab = new NoteLab(context.getApplicationContext());
    }
    return noteLab;
}


public static ArrayList<Note> getNotes(){
    return mNotes;
}

public static Note getNote(UUID mId){
    for (Note n: mNotes){
        if (mId == n.getnUUID()){
            return n;
        }
    }
    return null;
}

}

*NoteListFragment.java

package notes.com.example.alireza;


public class NoteListFragment extends ListFragment {

Note note;
ArrayList<Note> notes;
public static Date date;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    notes = NoteLab.get(getActivity()).getNotes();
    myArrayAdapter arrayAdapter = new myArrayAdapter(notes);
    setListAdapter(arrayAdapter);

}

@Override
public void onStart() {
    super.onStart();
    View emptyView = getActivity().getLayoutInflater().inflate(R.layout.emptymainlist,null);
    ((ViewGroup)getListView().getParent()).addView(emptyView);
    getListView().setEmptyView(emptyView);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    int checked = 1;
    Note c = ((myArrayAdapter)getListAdapter()).getItem(position);
    Intent i = new Intent(getActivity(),NoteActivity.class);
    i.putExtra("id",c.getnUUID());
    i.putExtra(NoteListActivity.EXTRA_DATE,checked);
    startActivity(i);
}

    //custom arrayList
public class myArrayAdapter extends ArrayAdapter<Note> {
    public myArrayAdapter(ArrayList<Note> notes) {
        super(getActivity(), 0, notes);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getActivity().getLayoutInflater().inflate(R.layout.notelistfragment_item, null);
        }
        Note n = getItem(position);
        date = n.getnDate();


        PersianCalendar persianCalendar = new PersianCalendar();
        persianCalendar.setTime(date);

        TextView titleTextView = convertView.findViewById(R.id.titleTextView);
        titleTextView.setText(n.getnTitle());

        TextView detailTextView = convertView.findViewById(R.id.detailsTextView);
        detailTextView.setText(n.getnDetails());

        TextView dateTextView = convertView.findViewById(R.id.dateTextView);
        dateTextView.setText(DateFormat.getDateInstance(DateFormat.FULL).format(date) + " : تاریخ ذخیره ");

        return convertView;
    }

}

@Override
public void onResume() {
    super.onResume();
    ((myArrayAdapter)getListAdapter()).notifyDataSetChanged();
}

as you understand from the codes, i want to save my mNotes arrayList which is placed in NoteLab.java.there is NoteListActivtiy.java too which is supporting NoteListFragment but i think this can be done in NoteListFragment.

thanks to stackoverflow and everyone who tryes to help me.

  • Possible duplicate of [Save ArrayList to SharedPreferences](https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) – Ravindra Shekhawat Dec 07 '18 at 13:48

1 Answers1

1

You can use Gson library to convert what you want to String then you can get it back again.

Where should you put your code ?? it depends on your business (or requirements).

If you want to pass your array of items to fragment or another activity you can use Gson lib to convert it to string then put it into bundle and pass this bundle to whatever you want. Then in the fragment or the activity you can get this array by Gson also.

Here is an example:

Gson gson = new Gson();
gson.toSring(yourData);

// then you can get the data by 
Gson gson = new Gson();
String data = yourBundle.getString("your_key");
yourDataModel = gson.fromJson(data, YourDataModel.class);

// it will work even with ArrayList
Khalid Ali
  • 373
  • 1
  • 15
  • yes i know how to use sharedPreference but my problem is i dont where to do it – Alireza Rashidi Dec 07 '18 at 16:05
  • It depends on where you expect to use it again. I am gonna give you two scenario first when user logged in successfully I save his data as soon as returned from the server, then when he comes back to the application he doesn't has to log in again. Another scenario when user add something to his favorite I save it into shared preference as soon as he click on the fav icon, or maybe a save button. so it depends on your business. – Khalid Ali Dec 10 '18 at 06:51