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