Android 在共享首选项中存储自定义数组列表
我想在共享首选项中存储自定义数组列表,以便下次打开应用程序时可以读取该列表。 我看了很多教程和答案,但我不能真正理解它 list_addr.javaAndroid 在共享首选项中存储自定义数组列表,android,arraylist,sharedpreferences,android-arrayadapter,custom-lists,Android,Arraylist,Sharedpreferences,Android Arrayadapter,Custom Lists,我想在共享首选项中存储自定义数组列表,以便下次打开应用程序时可以读取该列表。 我看了很多教程和答案,但我不能真正理解它 list_addr.java public class list_addr { public String title; public String detail; public list_addr( String title, String detail) { super(); this.title = title; this.detail=d
public class list_addr {
public String title;
public String detail;
public list_addr( String title, String detail) {
super();
this.title = title;
this.detail=detail;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return title + "\n" ;
}
}
list_adapter.java
enterpublic class list_addr_adapter extends ArrayAdapter<list_addr> {
Context context;
int layoutResourceId;
public list_addr_adapter(Context context, int layoutResourceId, List<list_addr> items) {
super(context, layoutResourceId, items);
this.layoutResourceId = layoutResourceId;
this.context = context;
// this.listener=callback;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtDetail;
ImageView imageview;
CheckBox checkbox;
}
ViewHolder holder = null;
public View getView(final int position, View convertView, ViewGroup parent) {
final list_addr lists = getItem(position);
final int pos=position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.items_sav_addr2, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle);
holder.txtDetail = (TextView) convertView.findViewById(R.id.detail);
holder.imageview = (ImageView) convertView.findViewById(R.id.imageview);
holder.checkbox=(CheckBox)convertView.findViewById(R.id.chkitem);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(lists.getTitle());
holder.txtDetail.setText(lists.getDetail());
return convertView;
}
}
只能在共享首选项中保存基元类型。如果需要保存ArrayList,则可以将其保存为逗号分隔的字符串。在获取此字符串时,请在此字符串上使用split(“,”),您将得到该字符串的字符串[] 如果你想保存一个对象列表,我建议你使用一个Singleton类。这是Singelton类的示例。如果您需要,请尝试此选项
public class ReferenceWrapper {
private Context context;
private static ReferenceWrapper wrapper;
private ArrayList<Object> list;
private ReferenceWrapper(Context context) {
this.context = context;
}
public static ReferenceWrapper getInstance(Activity activity) {
if (wrapper == null) {
wrapper = new ReferenceWrapper(activity);
}
return wrapper;
}
public ArrayList<Object> getList() {
return list;
}
public void setList(ArrayList<Object> list) {
this.list = list;
}
在任何活动中都能得到它
ReferenceWrapper wrapper=ReferenceWrapper.getInstance(MainActivity.this);
ArrarList<Object> list= wrapper.getList(yourArrarlist);
ReferenceWrapper=ReferenceWrapper.getInstance(MainActivity.this);
ArrarList list=wrapper.getList(您的ArrarList);
它将返回与您最近保存的ArrayList相同的列表。因为它只创建了一个对象
。请告诉我在共享首选项中保存arraylist是否有帮助。请执行以下操作
// Create List of address that you want to save
ArrayList addressList = new ArrayList();
addressList.add(new list_addr());
addressList.add(new list_addr());
SharedPreferences prefs = getSharedPreferences("address", Context.MODE_PRIVATE);
//save the user list to preference
Editor editor = prefs.edit();
try {
editor.putString("addressList", ObjectSerializer.serialize(addressList));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
然后使用以下命令检索arraylist
ArrayList addressList = new ArrayList();
// Load address List from preferences
SharedPreferences prefs = getSharedPreferences("address", Context.MODE_PRIVATE);
try {
addressList = (ArrayList) ObjectSerializer.deserialize(prefs.getString("addressList", ObjectSerializer.serialize(new ArrayList())));
} catch (IOException e) {
e.printStackTrace();
}
这是objectSerializer类的链接
答案是关于在
文件中写入对象,而不是共享引用。希望这能有所帮助
try {
ArrayList<List_addr> addrList = new ArrayList<>();
addrList.add(new List_addr("Bangalore", "Its a City"));
addrList.add(new List_addr("Delhi", "Its also a City"));
//write object into a file
FileOutputStream fos = openFileOutput("addrList", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(addrList);
oos.close();
//read object from the file
FileInputStream fis = openFileInput("addrList");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<List_addr> readAddrList = (ArrayList<List_addr>) ois.readObject();
ois.close();
for (List_addr address : readAddrList) {
Log.i("TAG", "Name " + address.getTitle() + " City " + address.getDetail());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
试试看{
ArrayList addrList=新的ArrayList();
addrList.add(新列表地址(“班加罗尔”,“它是一座城市”);
addrList.add(新德里,它也是一个城市);
//将对象写入文件
FileOutputStream fos=openFileOutput(“addrList”,Context.MODE\u PRIVATE);
ObjectOutputStream oos=新的ObjectOutputStream(fos);
oos.writeObject(addrList);
oos.close();
//从文件中读取对象
FileInputStream fis=openFileInput(“addrList”);
ObjectInputStream ois=新ObjectInputStream(fis);
ArrayList readAddrList=(ArrayList)ois.readObject();
ois.close();
用于(列表地址:readAddrList){
Log.i(“TAG”,“Name”+address.getTitle()+“City”+address.getDetail());
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}catch(classnotfounde异常){
e、 printStackTrace();
}
重要提示:请注意@MeetTitan回复评论 步骤1:
Step 1:
Put string array set into the shared preference.
pref.putStringSet(String key, Set<String> values);
Step 2:
Convert array list of pojo into set.
Set<String> set = new HashSet<String>(list);
Set 3:
Retrive using the getStringSet.
将字符串数组集放入共享首选项。
pref.putStringSet(字符串键,设置值);
步骤2:
将pojo的数组列表转换为集合。
Set Set=新哈希集(列表);
第三组:
使用getStringSet检索。
您是否尝试过将其保存在共享首选项中,因为您的代码没有显示任何一行保存内容code@VivekMishra不,我没有,因为我不理解这个方法。然后谷歌将arraylist存储在shared中preferences@VivekMishra我就是这么说的,让list\u addr
实现可序列化,并使用ObjectOutputStream
和ByteArrayOutputStream
将该数组列表序列化为字节数组,然后存储字节数组,并在使用时将其反序列化回列表。如果您感到困惑,我可以添加一个代码示例。如何准确地使用这个类?
try {
ArrayList<List_addr> addrList = new ArrayList<>();
addrList.add(new List_addr("Bangalore", "Its a City"));
addrList.add(new List_addr("Delhi", "Its also a City"));
//write object into a file
FileOutputStream fos = openFileOutput("addrList", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(addrList);
oos.close();
//read object from the file
FileInputStream fis = openFileInput("addrList");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<List_addr> readAddrList = (ArrayList<List_addr>) ois.readObject();
ois.close();
for (List_addr address : readAddrList) {
Log.i("TAG", "Name " + address.getTitle() + " City " + address.getDetail());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Step 1:
Put string array set into the shared preference.
pref.putStringSet(String key, Set<String> values);
Step 2:
Convert array list of pojo into set.
Set<String> set = new HashSet<String>(list);
Set 3:
Retrive using the getStringSet.