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

        }

    }

}

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

15 Comments

I have changed the code check UPDATE, but still its replacing the with the latest value.
isnt the code adding "d1" object only? [it passes to toStringList with for-each, good to see that use, but are you passing more?]
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
its crashing at OnClick after i changed it to myStringList.addAll(toStringList(entryLogs));
it a public method in the Monday_fragment class public ArrayList<String> toStringList(Collection<DiaryLogs> entryLogs) { ArrayList<String> stringList = new ArrayList<String>(); for (DiaryLogs myobj : entryLogs) { stringList.add(myobj.toString()); } return stringList; }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.