以共享首选项保存arraylist [英] save arraylist in shared preference
问题描述
我将值存储在ArrayList
中,并将其传递给使用束的下一个Fragment
,在那里我将值设置为我的TextView
,直到在这里它可以正常工作,现在转到另一页并进入应用程序并再次返回到Fragment
,我所有的数据都保存完了,所以我试图将其存储在首选项中,但首选项不允许访问ArrayList
,以下是我的代码
I am storing values in ArrayList
and pass it to using bundle to next Fragment
, and there I set values to my TextView
, till here it works fine, now when go to another page and proceed to app and come back again to that Fragment
, my all data goes, so I am trying to store it in preferences but preference don't allow to access ArrayList
, following is my code
public class Add_to_cart extends Fragment {
private Button continue_shopping;
private Button checkout;
ListView list;
private TextView _decrease,mBTIncrement,_value;
private CustomListAdapter adapter;
private ArrayList<String> alst;
private ArrayList<String> alstimg;
private ArrayList<String> alstprc;
private String bname;
private ArrayList<String> alsttitle;
private ArrayList<String> alsttype;
public static ArrayList<String> static_Alst;
public Add_to_cart(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.list_view_addtocart, container, false);
alst=new ArrayList<String>();
alstimg=new ArrayList<String>();
Bundle bundle = this.getArguments();
alst = bundle.getStringArrayList("prducts_id");
alsttype = bundle.getStringArrayList("prducts_type");
alstimg=bundle.getStringArrayList("prducts_imgs");
alsttitle=bundle.getStringArrayList("prducts_title");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
System.out.println("TEst--" + alst);
// Toast.makeText(getActivity(),"Testing"+alstimg,Toast.LENGTH_LONG).show();
list=(ListView)rootView.findViewById(R.id.list_addtocart);
adapter = new CustomListAdapter(getActivity(),alst,alstimg,alsttitle,alsttype);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// TODO Auto-generated method stub
}
});
return rootView;
}
public class CustomListAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> listData;
private ArrayList<String> listDataimg;
private ArrayList<String> listDatatitle;
private ArrayList<String> listDatatype;
private AQuery aQuery;
String dollars="\u0024";
public CustomListAdapter(Context context,ArrayList<String> listData,ArrayList<String> listDataimg,ArrayList<String> listDatatitle,ArrayList<String> listDatatype) {
this.context = context;
this.listData=listData;
this.listDataimg=listDataimg;
this.listDatatitle=listDatatitle;
this.listDatatype=listDatatype;
aQuery = new AQuery(this.context);
}
public void save_User_To_Shared_Prefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(listData);
Add_to_cart.static_Alst=listData;
prefsEditor.putString("user", json);
prefsEditor.commit();
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
holder.propic = (ImageView) convertView.findViewById(R.id.img_addtocart);
holder.txtproname = (TextView) convertView.findViewById(R.id.proname_addtocart);
holder.txtprofilecast = (TextView) convertView.findViewById(R.id.proprice_addtocart);
holder.txtsize = (TextView) convertView.findViewById(R.id.txt_size);
_decrease = (TextView) convertView.findViewById(R.id.minuss_addtocart);
mBTIncrement = (TextView) convertView.findViewById(R.id.plus_addtocart);
_value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
mBTIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
increment();
}
});
_decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
decrement();
}
});
holder.txtprofilecast.setText(dollars+listData.get(position));
holder.txtproname.setText(listDatatitle.get(position));
holder.txtsize.setText(listDatatype.get(position));
System.out.println("Image ka array " + listDataimg.get(position));
//Picasso.with(mContext).load(mThumbIds[position]).centerCrop().into(imageView);
// Picasso.with(context).load(listDataimg.get(position)).into(holder.propic);
aQuery.id(holder.propic).image(listDataimg.get(position), true, true, 0, R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
ImageView propic;
TextView txtproname;
TextView txtprofilecast;
TextView txtsize;
}
}
}
推荐答案
首先从下面的链接下载Gson.jar
,然后将其添加到项目的libs
文件夹中
First download Gson.jar
from below link and then add it to libs
folder of your project
http://www.java2s.com/Code/Jar/g/Downloadgson17jar.htm
然后将那个ArrayList
放到Class中,并创建该类的对象,然后可以将该对象保存到SharedPreferences
中,如下所示:
then put That ArrayList
in the Class and make object of that class then you can save that object to SharedPreferences
like below
public static void save_User_To_Shared_Prefs(Context context, User _USER) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(_USER);
prefsEditor.putString("user", json);
prefsEditor.commit();
}
上面的代码是_USER objext包含ArrayList
的示例.
above code is an example _USER objext contain ArrayList
.
要读取对象,请看下面的代码
And to read the object have a look at below code
public static User get_User_From_Shared_Prefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Gson gson = new Gson();
String json = appSharedPrefs.getString("user", "");
User user = gson.fromJson(json, User.class);
return user;
}
现在,当您想获取_USER对象时,请调用上述函数,结果您将拥有该对象,并且您将拥有ArrayList
now when you want to get the _USER object call the above function and in result you will have the object and in that you will have the ArrayList
这篇关于以共享首选项保存arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!