0

did debugging. I can clearly see after entryLogs.add(dl); in the value of entryLogs. the first data is replaced and a second has been inserted to the ArrayList.

what is happening is every time i enter new value it adds it in the ArrayList but replaces the the first entry to be same as the second entry. for example: I add A and onclick it adds A. again, add B and onclick it add B. but in the Arraylist the value is [17/4/2014:20:29 B, 17/4/2014:20:29 B]

Please suggest what needs to be done. Entire code can be found here: https://github.com/tirthoguha/DroidProject/blob/myDiary/src/com/example/s0217980_diary/Monday_fragment.java

entryLogs = new ArrayList<DiaryLogs>();

            timeText = (EditText) getView().findViewById(R.id.dateTimeEText);

            entryText = (EditText) getView().findViewById(R.id.diaryEntryEText);

            Button saveBtn = (Button) getView()
                    .findViewById(R.id.saveDiaryEntryBtn);
            saveBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    timeEntry = timeText.getText().toString();

                    entryEntered = entryText.getText().toString();

                    dl = new DiaryLogs(1, timeEntry, entryEntered);

                    entryLogs.add(dl);
t_godd
  • 194
  • 2
  • 3
  • 18

3 Answers3

2

In the class DiaryLogs.java you declare the strings timeEntry and entryEntered as static

static String timeEntry;
static String entryEntered;

Remove the static modifier and you'll be fine. Static objects will have the same value for any instance of that class (and are accessible even without having an instance of it), so if you set the value in another object, every instance will use that value.

Smokez
  • 382
  • 1
  • 5
  • you are legend, my friend :) – t_godd Apr 18 '14 at 12:16
  • fixed, that part. But came into a new problem in first onClick in Monday_fragment it add the data to ArrayList. Second OnClik in Monday_fragment it takes to Monday_list_fragment where the list is shown. There is a button which brings it back to the Monday_fragment. I debugged an seen, when i came back all the data in ArrayList is gone. How to keep the ArrayList data not to clear out on coming back. Codes here: https://github.com/tirthoguha/DroidProject/tree/myDiary/src/com/example/s0217980_diary – t_godd Apr 18 '14 at 18:26
  • You must explicitly store the state of the arraylist when you are changing orientation or activity, if you want to use that data later on. You could check out http://stackoverflow.com/questions/12503836/how-to-save-custom-arraylist-on-android-screen-rotate to implement this. I know the link is about rotating screens, but the same thing is happening when you move away from the activity or fragment. Another option is to create a class that extends Application, and declare the arraylist there. The problem then is that all classes can access it. – Smokez Apr 22 '14 at 14:03
0

Maybe you should remove the line 64

entryLogs = new ArrayList<DiaryLogs>();

and initialize the ArrayList on the line 22

ArrayList<DiaryLogs> entryLogs;

Maybe it helps :)

Aksiom
  • 1,565
  • 4
  • 25
  • 39
0

Put these two lines

timeText = (EditText) getView().findViewById(R.id.dateTimeEText);

        entryText = (EditText) getView().findViewById(R.id.diaryEntryEText);

in @Override public void onClick(View v) { }

May this will help.