Java 我的项目在保存ArrayList时遇到问题<;T>;共享参考

Java 我的项目在保存ArrayList时遇到问题<;T>;共享参考,java,android,arrays,arraylist,sharedpreferences,Java,Android,Arrays,Arraylist,Sharedpreferences,我有一个让我发疯的问题。 我知道如何在android中使用“SharedReferences”,但我不知道在我的项目中放在哪里,比如onCreate()、onDestroy()或其他地方。 *我想在NoteListFragment中保存NoteLab.mNotes *Note.java package notes.com.example.alireza; public class Note { public UUID nId; public Date nDate; public String n

我有一个让我发疯的问题。 我知道如何在android中使用“SharedReferences”,但我不知道在我的项目中放在哪里,比如onCreate()、onDestroy()或其他地方。 *我想在NoteListFragment中保存NoteLab.mNotes

*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;
}

}
package notes.com.example.alireza;
导入android.content.Context;
导入java.util.ArrayList;
导入java.util.UUID;
课堂记事本{
公共静态数组列表mNotes;
公共静态NoteLab NoteLab;
公共上下文映射上下文;
私有NoteLab(上下文AppContext){
mAppContext=AppContext;
mNotes=newarraylist();
}
公共静态NoteLab get(上下文){
如果(noteLab==null){
noteLab=新的noteLab(context.getApplicationContext());
}
返回noteLab;
}
公共静态数组列表getNotes(){
返回mNotes;
}
公共静态注释getNote(UUID mId){
适用于(注:mNotes){
如果(mId==n.getnUUID()){
返回n;
}
}
返回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();
}
package notes.com.example.alireza;
公共类NoteListFragment扩展了ListFragment{
注释;
ArrayList注释;
公共静态日期;
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
notes=NoteLab.get(getActivity()).getNotes();
myArrayAdapter arrayAdapter=新myArrayAdapter(注释);
setListAdapter(arrayAdapter);
}
@凌驾
public void onStart(){
super.onStart();
视图emptyView=getActivity().GetLayoutFlater().inflate(R.layout.emptymainlist,null);
((视图组)getListView().getParent()).addView(emptyView);
getListView().setEmptyView(emptyView);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
整数=1;
注c=((myArrayAdapter)getListAdapter()).getItem(位置);
Intent i=新的Intent(getActivity(),NoteActivity.class);
i、 putExtra(“id”,c.getnUUID());
i、 putExtra(NoteListActivity.EXTRA\u日期,选中);
星触觉(i);
}
//自定义阵列列表
公共类myArrayAdapter扩展了ArrayAdapter{
公共myArrayAdapter(ArrayList注释){
super(getActivity(),0,notes);
}
@SuppressLint(“SetTextI18n”)
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=getActivity().GetLayoutFlater().inflate(R.layout.notelistfragment_项,空);
}
注n=获取项目(位置);
日期=n.getnDate();
PersianCalendar PersianCalendar=新的PersianCalendar();
设置时间(日期);
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)+“:日期;
返回视图;
}
}
@凌驾
恢复时公开作废(){
super.onResume();
((myArrayAdapter)getListAdapter()).notifyDataSetChanged();
}
正如您从代码中了解到的,我想保存我的mNotes arrayList,它放在NoteLab.java中。还有NoteListActivtiy.java,它也支持NoteListFragment,但我认为这可以在NoteListFragment中完成


感谢stackoverflow和所有试图帮助我的人。

您可以使用Gson库转换您想要的字符串,然后您可以再次获取它

您应该将代码放在哪里?这取决于您的业务(或需求)

如果您想将项目数组传递给fragment或其他活动,您可以使用Gson lib将其转换为字符串,然后将其放入捆绑包中,并将此捆绑包传递给您想要的任何对象。然后在片段或活动中,您也可以通过Gson获得此数组

以下是一个例子:

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

是的,我知道如何使用SharedReference,但我的问题是我不知道在哪里使用它取决于您希望在哪里再次使用它。我将给您两种情况:首先,当用户成功登录时,我会在从服务器返回时立即保存其数据,然后当他返回应用程序时,他不必再次登录。A另一种情况是,当用户将某个内容添加到他的收藏夹中时,我会在他单击fav图标或“保存”按钮时将其保存到共享首选项中。因此,这取决于您的业务。