0

I have an EditText in a fragment where user enters something and OnClick the data is added to a custom ArrayList. Then another button which takes to a ListView fragment where all data should be listed but can only see the latest item entered by the user.

Code for adding data in button's OnClick, I think this is where I am doing wrong

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

                EditText entryText = (EditText) getView().findViewById(
                        R.id.diaryEntryEText);
                String timeEntry = timeText.getText().toString();

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

                ArrayList<DiaryLogs> entryLogs = new ArrayList<DiaryLogs>();

                DiaryLogs dl = new DiaryLogs(1, timeEntry, entryEntered);
                entryLogs.add(dl);

Other codes below

Custom Object Class DiaryLogs

public class DiaryLogs {

    //public static ArrayList<DiaryLogs> entryLogs;

    String timeEntry, entryEntered;
    int day;

    // single constructor that takes an integer and two string
    public DiaryLogs(int day, String timeEntry, String entryEntered) {
        super();
        this.day = day;
        this.timeEntry = timeEntry;
        this.entryEntered = entryEntered;

    }

    public String getTimeEntry() {
        return timeEntry;
    }

    public void setTimeEntry(String timeEntry) {
        this.timeEntry = timeEntry;
    }

    public String getEntryEntered() {
        return entryEntered;
    }

    public void setEntryEntered(String entryEntered) {
        this.entryEntered = entryEntered;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.timeEntry + "\n" + this.entryEntered;


    }


}

UPDATE Class Monday_fragment

public class Monday_fragment extends Fragment {

    public ArrayList<String> myStringList;
    Bundle bundle;
    ArrayList<DiaryLogs> entryLogs;
    EditText timeText;
    EditText entryText;
    DiaryLogs dl;
    String timeEntry;
    String entryEntered;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater.inflate(R.layout.monday_fragment, container, false);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        currentDateTime();
        super.onViewCreated(view, savedInstanceState);

    }

    public void currentDateTime() {
        EditText timeText = (EditText) getView().findViewById(
                R.id.dateTimeEText);
        SimpleDateFormat df = new SimpleDateFormat("d/M/yyyy:H:m");
        String dateTime = df.format(Calendar.getInstance().getTime());
        timeText.setText(dateTime);
    }

    public ArrayList<String> toStringList(Collection<DiaryLogs> entryLogs) {
        ArrayList<String> stringList = new ArrayList<String>();

        for (DiaryLogs myobj : entryLogs) {
            stringList.add(myobj.toString());
        }

        return stringList;
    }

    @Override
    public void onStart() {
        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);
                //convert entryLogs to string array list
                myStringList = toStringList(entryLogs);

                Toast.makeText(getActivity(), "Entry added \n" + dl,
                        Toast.LENGTH_SHORT).show();
                        entryText.setText("");






            }

        }

        );
        System.out.println(entryLogs);

        Button showBtn = (Button) getView().findViewById(
                R.id.showDiaryEntriesBtn);
        showBtn.setOnClickListener(new View.OnClickListener() {

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

                if (myStringList != null) {
                    bundle = new Bundle();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    Monday_list_fragment mlf = new Monday_list_fragment();

                    bundle.putStringArrayList("list", myStringList);
                    mlf.setArguments(bundle);

                    fragmentTransaction.replace(android.R.id.content, mlf);
                    fragmentTransaction.commit();
                }
                if (myStringList == null) {
                    Toast.makeText(getActivity(),
                            "No entry have been added yet", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });

        super.onStart();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

}

Class Monday_list_fragment

        public class Monday_list_fragment extends ListFragment {
    ArrayList<String> logs;
    Bundle bundle;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater
                .inflate(R.layout.monday_list_fragment, container, false);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        bundle = this.getArguments();
        if (bundle != null) {
            logs = bundle.getStringArrayList("list");

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    getActivity(), android.R.layout.simple_list_item_1, logs);
            setListAdapter(adapter);

        }

    }

}
t_godd
  • 194
  • 2
  • 3
  • 18

1 Answers1

1
ArrayList<DiaryLogs> entryLogs = new ArrayList<DiaryLogs>();  

Make that object a global variable. In the activity/fragment with your edittext, Break it into:

ArrayList<DiaryLogs> entryLogs;

And

shift the entryLogs = new ArrayList<DiaryLogs>(); part to -on activity or fragment creation.

Right now what is happening is that the entryLogs arrayList is being created every time you enter the data. So you can see only the last entry in it.

When you add() objects to a global arrayList, it'l incrementally add every object.

And for your implementation, i think the myStringList should be initialized, populated in the same way as the entryLogs list. So keep ADDING objects to myStringList instead of the assigning function.

In that case, the change has to be like:

myStringList = toStringList(entryLogs);  

needs to be:

myStringList.addAll(toStringList(entryLogs));
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • I have changed the code check **UPDATE**, but still its replacing the with the latest value. – t_godd Apr 18 '14 at 07:13
  • isnt the code adding "d1" object only? [it passes to toStringList with for-each, good to see that use, but are you passing more?] – Pararth Apr 18 '14 at 07:24
  • yes, but what I want is to create new dl on new onClick and keep adding to the exiting ArrayList then the entire ArrayList is converted to String Array using method toStringList, as I have to pass the arguments through bundle to the Monday_list_fragment's ListView – t_godd Apr 18 '14 at 07:29
  • its crashing at OnClick after i changed it to `myStringList.addAll(toStringList(entryLogs));` – t_godd Apr 18 '14 at 07:46
  • it a public method in the Monday_fragment class `public ArrayList toStringList(Collection entryLogs) { ArrayList stringList = new ArrayList(); for (DiaryLogs myobj : entryLogs) { stringList.add(myobj.toString()); } return stringList; }` – t_godd Apr 18 '14 at 08:38
  • And you can also see all the code here https://github.com/tirthoguha/DroidProject – t_godd Apr 18 '14 at 08:44
  • check the link now, sorry for the inconvenience – t_godd Apr 18 '14 at 08:54
  • you need to debug and check it first, see when/where the values are passed and/or turn null – Pararth Apr 18 '14 at 09:11
  • i debugged it, 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 add a. again, add b and onclick it add b. but in the Arralist [17/4/2014:20:29 b, 17/4/2014:20:29 b] Suggest what can be done – t_godd Apr 18 '14 at 09:48
  • myStringList = toStringList(entryLogs); --don't do that everytime you add object, do it only before you pass myStringList. Remove that line from saveBtn's onclicklistener – Pararth Apr 18 '14 at 11:06
  • I comment out 'myStringList = toStringList(entryLogs);' and debugged. 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. – t_godd Apr 18 '14 at 11:35
  • remove the constructor from DiaryLogs and just use getter setters, i think when you "add" the object, it invokes the constructor – Pararth Apr 18 '14 at 12:01
  • i fixed it, Thanks for all your help. the variable timeEntry & entryEntered were declared static in class DiaryLogs. that's why all the trouble. But came into a new problem in first onClick it add the data to ArrayList. second onlcik to get List, first converts the custom ArrayList to String ArrayList, then puts it in bundle and replace fragment to show list. But even if i don't add new data, i wan't to show all the existing in custom ArrayList. How do I do that. Codes here: https://github.com/tirthoguha/DroidProject/tree/myDiary/src/com/example/s0217980_diary – t_godd Apr 18 '14 at 16:07
  • bro, all this stuff's working except this new thing. If you can help me please. http://stackoverflow.com/questions/23153583/on-add-previous-element-getting-replaced-in-arraylist – t_godd Apr 18 '14 at 18:50
  • don't have so much time for the next few hours, 'l look later, you can try posting a new question with relevant code and problem, will get help – Pararth Apr 19 '14 at 03:28