Search code examples
androidjsonlistviewarraylistdata-retrieval

Trouble getting data from json file back


So I managed to save data from an array in a json file. This array is created manually as you can see by the 8 item.add(...) and each item has 4 values. isSelected, userName, userAttack, userStatus and isFavorite. Only isSelected and isFavorite will change through onItemClickListeners and are of type boolean. I want to store the change of those values as soon as I click on the item and it worked. The Logs would show me that json is an array with all the information inside.

My Problem is the retrieval of this information when the app is created and I have no idea where to place it. Not only that but I dont know how to use the json file in onCreate because the json is only made when I click or longclick on the items. Can somebody give me hints on how to successfully let the app remember that values the item has? Would be very thankful.

    //package and imports 

    public class MainActivity extends Activity {

    ListView listView;
    private String msg = "Android : ";
    private boolean isChecked;

    ArrayList<UserModel> item = new ArrayList<>();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ArrayList<UserModel> item = new ArrayList<>();
        listView = (ListView) findViewById(R.id.list_view);


            item.add(new UserModel(false, R.string.name0, R.string.atk0, R.string.stat0,false));
            item.add(new UserModel(false, R.string.name1, R.string.atk1, R.string.stat1,false));
            item.add(new UserModel(false, R.string.name2, R.string.atk2, R.string.stat2,false));
            item.add(new UserModel(false, R.string.name3, R.string.atk3, R.string.stat3,false));
            item.add(new UserModel(false, R.string.name4, R.string.atk4, R.string.stat4,false));
            item.add(new UserModel(false, R.string.name5, R.string.atk5, R.string.stat5,false));
            item.add(new UserModel(false, R.string.name6, R.string.atk6, R.string.stat6,false));
            item.add(new UserModel(false, R.string.name7, R.string.atk7, R.string.stat7,false));

            Log.d(msg, "Made UserModel List with size: " + item.size());


        final CustomAdapter adapter = new CustomAdapter(this, item);
        listView.setAdapter(adapter);


        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(msg,"Item " + i + " was long clicked!");

                UserModel model = item.get(i);

                if(model.isFavorite()) {
                    model.setFavorite(false);
                    Log.d(msg, "Item " + i + " was removed as Favorite!");
                    Toast.makeText(view.getContext(),  model.getUserName() + i + " was removed from Favorites!", Toast.LENGTH_SHORT).show();
                } else {
                    model.setFavorite(true);
                    Log.d(msg, "Item " + i + " was added as Favorite!");
                    Toast.makeText(view.getContext(),model.getUserName() + i + " was added to Favorites!",Toast.LENGTH_SHORT).show();
                }

                item.set(i, model);

                adapter.updateRecords(item);

                Gson gson = new Gson();
                String json = gson.toJson(item);

                Log.d(msg, "Here is the json file: " + json);


                return model.isFavorite();
                }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Log.d(msg, "Item " + (i) + " was clicked!");
                UserModel model = item.get(i);


                if (model.isSelected())
                    model.setSelected(false);
                else
                    model.setSelected(true);
                    Log.d(msg, "Item " + (i) + " " + model.isSelected());

                item.set(i, model);

                //now update adapter
                adapter.updateRecords(item);

                Gson gson = new Gson();
                String json = gson.toJson(item);

                Log.d(msg, "Here is the json file: " + json);

            }



        });

    }

}

Solution

  • Assuming I understood your question, here is how you can load the json data onCreate.

    ArrayList<UserModel> items = new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Load json to string from the file you saved it in
        ...
        String rawJson = "...";
    
        //Parse data to your list
        ...
        items = gson.fromJson(br, new TypeToken<List<UserModel>>(){}.getType());
    }
    

    However, I would recommend looking into storing your data in a local database, as it is easier to update and manage. Storing json in a file is very ugly.

    You could check out libraries as Room.