0

This is my main activity so far

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
private List<userTask>  tasklist= new ArrayList<>();
private userTaskAdapter adapter;
//private Context context;
private static Bundle mBundleState;
private final String KEY_RECYCLER_STATE = "recycler_state";
private Parcelable listState;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycleOne);

    recyclerView.setHasFixedSize(true);

    adapter = new userTaskAdapter(tasklist);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);
    preparetestdata();

}

private void preparetestdata()
{
    String data = "";


    try {

        InputStream fis = MainActivity.this.getResources().getAssets().open("task.txt");
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        while ((data = br.readLine()) != null)
        {
            String delims = "[:]+";
            String[] temp = data.split(delims);
            String tempTask = new String();
            String tempDescription = new String();
            for (int i=0; i < temp.length; i++)
            {
                if(i == 0)
                    tempTask = temp[i];
                else
                    tempDescription = temp[i];
            }
            userTask tempUser = new userTask(tempTask, tempDescription);
            tasklist.add(tempUser);
        }

    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

/**
 * This function will add new task to task list
 * @param v
 */
public void onAdd(View v)
{
    EditText editTaskTitle = (EditText) findViewById(R.id.taskTitle);
    EditText editDescription = (EditText) findViewById(R.id.taskDescription);
    String tname = editTaskTitle.getText().toString();
    String tdescrip = editDescription.getText().toString();
    userTask task = new userTask(tname, tdescrip);
    tasklist.add(task);
    Log.d("Tname :", tname);
    Log.d("tdescription :", tdescrip);
    try {
        FileOutputStream stream = new FileOutputStream(new File(getFilesDir(), "task.txt"));

        int counter =0;
        for (int i =0; i < tasklist.size(); i++)
        {

            String total = tasklist.get(i).getTName() + " : " + tasklist.get(i).getTDescrip() + "\n";
            stream.write(total.getBytes());
            counter++;
        }
        Toast.makeText(this, "got here", Toast.LENGTH_SHORT).show();
        stream.close();
        adapter.notifyDataSetChanged();
        //recyclerView
        Log.d("UpList", String.valueOf(counter));
    }catch(IOException e) {
        e.printStackTrace();
    }
}




/**
@Override
protected void onSaveInstanceState(Bundle state)
{
    super.onSaveInstanceState(state);
    //state.putSerializable("listdata", adapter.);
}**/

@Override
public void onStop()
{
    super.onStop();
    mBundleState = new Bundle();
    listState = recyclerView.getLayoutManager().onSaveInstanceState();
    mBundleState.putParcelable(KEY_RECYCLER_STATE, listState);

}


@Override
public void onResume()
{
    super.onResume();
    if (mBundleState != null)
    {
        listState = mBundleState.getParcelable(KEY_RECYCLER_STATE);
        recyclerView.getLayoutManager().onRestoreInstanceState(listState);
    }
}

When I add a new task and exits the app that new task will still be in the recycler view. But when I close the app that new task will not still be in the recycler view. I was thinking of saving the state of the adapter but am not really sure how to do this. I tried to do the serializable method but adapters are not serialable

alpha245
  • 1
  • 1
  • 2
  • Does this answer your question? [How to save data in an android app](https://stackoverflow.com/questions/10962344/how-to-save-data-in-an-android-app) – Ryan M Feb 06 '20 at 23:47
  • It is unclear what you consder to be the state of the adapter. But all info is in the tasklist and hence saved to task.txt in getFilesDir(). So at start of app just load that file instead the one from assets. – blackapps Feb 07 '20 at 02:12
  • `When I add a new task and exits the app that new task will still be in the recycler view. ` ??? All will be gone then. `But when I close the app that new task will not still be in the recycler view. `. . Indeed. But exit and close is the same... – blackapps Feb 07 '20 at 02:15
  • How would I change the files from the one in the asset to the one getFilesDir() I know I would have to change the InputStream in the function preparetestdata() – alpha245 Feb 07 '20 at 02:35
  • That all I needed to do it works perfectly now – alpha245 Feb 07 '20 at 02:42

0 Answers0