0

INTRO: I am trying to pass an array with sub arrays into another activity using sharedpreferences.

WHAT I WANT TO ACHIEVE: I have an array of data gotten from an api of which I display as a list on a particular activity. This is fine as it works.

But I want that on item click, it saves the item data and loads in on another activity. Then on the new activity, I can add new data to that already saved information before closing the activity which then saves the data back to the database on the server.

REASON: I am doing it this way because of slow network connection and smooth page loading. I am trying to use SharedPreferences to save the array on item click, load it up in another activity and then add more data to the already saved array on SharedPreferences.

CURRENT STATE: At the moment, I can only save and get a single variable, not an array.

This is the array:

 {  "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"}
                }]}

This is the code

    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) {

            }
        }
    });

I would gladly go with a better way of achieving this. I just want to avoid loading the data from Api all the time.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ayomikun Samuel
  • 125
  • 2
  • 9

1 Answers1

0

Make an OutputData object and make it Parcelable, then use Intent.putExtra() while starting another Activity. It's the right way to pass objects from Activity to Activity, you should not use SharedPreferences. Check out this link.

MJakhongir
  • 2,101
  • 2
  • 16
  • 27