我想在共享首选项中存储自定义数组列表,以便下次打开我的应用程序时可以读取列表。 我查看了许多教程和答案,但我真的无法理解它。
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=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;
}
}
这就是我对列表视图进行充气的方法 -
listView1 = (ListView) findViewById(R.id.listView);
list_adapter_invoice adapter = new list_adapter_invoice(this,
R.layout.items_row, MyAdaptertwo.rowitems);
listView1.setAdapter(adapter);
答案 0 :(得分:0)
您只能在共享首选项中保存原始类型。如果需要保存ArrayList,则可以将其保存为以逗号分隔的String。在获取此用法时,在此字符串上使用split(&#34;,&#34;)并且您将获得它的String []。
如果你想保存一个Object列表,那么我建议使用Singleton类。这是Singelton Class的例子。试试这个你想要的。
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);
wrapper.setList(yourArrarlist);
在任何活动中获取它
ReferenceWrapper wrapper=ReferenceWrapper.getInstance(MainActivity.this);
ArrarList<Object> list= wrapper.getList(yourArrarlist);
它将返回您最近保存的相同ArrayList列表。因为它只有一个对象是Created 让我知道是否有帮助
答案 1 :(得分:0)
答案是关于File
而不是SharedPreference
中的写对象。希望这可能会有所帮助
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();
}
重要提示:请介意@MeetTitan回答评论。
答案 2 :(得分:0)
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.
答案 3 :(得分:-1)
要在共享偏好设置中保存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类的链接 Object Serializer