[英]passing Array from Api to to another activity using SharedPreferences and adding to already saved array
簡介:我正在嘗試使用sharedpreferences將帶有子數組的數組傳遞到另一個活動中。
我想實現的目標 :我有一個從api中獲得的數據數組,這些數據顯示為特定活動的列表。 這很好,因為它可以工作。
但是我希望在單擊項目時保存項目數據並加載到另一個活動中。 然后,在新活動上,我可以在關閉活動之前將新數據添加到已保存的信息中,然后再將數據保存回服務器上的數據庫中。
原因:我這樣做是因為網絡連接速度慢和頁面加載順暢。 我正在嘗試使用SharedPreferences在項目單擊時保存數組,將其加載到另一個活動中,然后將更多數據添加到SharedPreferences上已保存的數組中。
當前狀態:目前,我只能保存並獲取一個變量,而不是數組。
這是數組:
{ "OutputCode":1,
"OutputMessage":"Success",
"OutputData":[
{
"id":"1",
"status":"1",
"date_borrowed":"2018-01-16 16:34:01",
"date_returned":"0000-00-00 00:00:00",
"staff_id":"1",
"student_id": "1",
"log_contents":[{ "id":"8",
"log_id":"1",
"book_id":"1",
"quantity":"3",
"book_info":{ "id":"1",
"name":"Algebra",
"author":"John Smith",
"ISBN":"987-554-3243"}},
{ "id": "5",
"log_id": "1",
"book_id": "1",
"quantity": "2",
"book_info": { "id": "1",
"name": "C++",
"author": "Jane Smith",
"ISBN": "987-656-3870"}}],
"student_details":{"id":"1",
"fname":"John",
"lname":"James",
"username":"username",
"password":"password",
"date_created":"2018-01-15 19:53:41",
"phone":"+15673566"}
},
{
"id": "2",
"status": "0",
"date_borrowed": "2018-01-13 16:34:01",
"date_returned": "2018-01-14 17:10:14",
"staff_id": "1",
"student_id": "1",
"log_contents": [],
"student_details": {"id": "2",
"fname": "Mary",
"lname": "Jane",
"username": "username",
"password": "password",
"date_created": "2018-01-15 20:53:41",
"phone": "+14768986"}
}]}
這是代碼
access.runCommand("get_all_bookings", keys, values, new AtomAccess.Callback() {
@Override
public void onSuccess(JSONObject output) {
try {
String outputCode = output.getString("outputCode");
String outputMessage = output.getString("outputMessage");
updtedlist = (ListView) findViewById(R.id.lib_book_list_view);
if (outputCode.equals("1")) {
JSONArray outputData = output.getJSONArray("outputData");
for (int i = 0; i < outputData.length(); i++) {
JSONObject obj = outputData.getJSONObject(i);
JSONObject stu = obj.getJSONObject("student_details");
Student bookBorrower = new Student(
stu.getString("id"),
stu.getString("fname"),
stu.getString("lname"),
stu.getString("username"),
stu.getString("password"),
stu.getString("date_created"),
stu.getString("phone"));
JSONArray cont = obj.getJSONArray("log_content");
ArrayList<Borrow> contents = new ArrayList<Borrow>();
for (int j = 0; j < cont.length(); j++) {
JSONObject con = cont.getJSONObject(j);
JSONObject buk = con.getJSONObject("book_info");
contents.add(new Borrow(
con.getString("id"),
con.getString("log_id"),
con.getString("book_id"),
con.getString("quantity"),
new Book(buk.getString("id"),
buk.getString("name"),
buk.getString("author"),
buk.getString("ISBN"))
));
}
Log log = new Log(
obj.getString("id"),
obj.getString("status"),
obj.getString("date_borrowed"),
obj.getString("date_returned"),
obj.getString("staff_id"),
obj.getString("student_id"),
contents,
bookBorrower);
open_logs.add(log);
}
}
updtedlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
Log LogItem = (Log) adapter.getItem(position);
Student student = LogItem.getbookBorrower();
ArrayList<Borrow> buro = new ArrayList<Borrow>();
ArrayList<Book> buk = new ArrayList<Book>();
buro = LogItem.getContents(); //Here, am trying to get array of model 'Borrow' and 'Book'
Log.v("buro :", ""+buro); // the result here is [com.samuel.booking.entity.Product@e8f6200, com.samuel.booking.entity.Product@aad103]
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("fang", ""+student.getFirstName()); // This return the right info
editor.putString("lang", ""+buro); // This yields no result except that in the log above
editor.apply();
//I was able to get the variable below
Intent i = new Intent(LogsActivity.this, LogInfoActivity.class);
// i.putExtra("KEY_log_ID", LogItem.getId());
// i.putExtra("KEY_Log_STATUS", LogItem.getStatus());
// i.putExtra("KEY_Los_Student_ID", student.getId());
// i.putExtra("KEY_Log_Student_FNAME", student.getFirstName())
startActivity(i);
}
});
adapter = new LogsAdapter(LogsActivity.this, open_Logs);
updtedlist.setAdapter(adapter);
removeLoadingOverlay();
} catch (JSONException e) {
}
}
});
我很樂意采用更好的方法來實現這一目標。 我只是想避免一直從Api加載數據。
做一個OutputData
對象,並使其Parcelable ,然后使用Intent.putExtra()
而開始另一個Activity
。 這是將對象從Activity
傳遞到Activity
的正確方法,您不應使用SharedPreferences
。 查看此鏈接 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.