Objective:
Create a new class (diaryLogs) with these fields to hold the values and a single constructor that takes an int (0 Mon, 1 Tue, 2 Wed etc) and two strings for the date time and diary entry to initialise an instance of the object. As a number of entries will be made for the same day, use an ArrayList < diaryLogs > to store the values. As this value will be shared between fragments declare the variable as static in the MainActivity class. Use the .add() method of ArrayList to add the new diaryLog to the ArrayList in your setOnClickListener() method. Repeat this operation for the other diary page fragments.
The monday_list fragment consists of a ListView and a button used to return to the Monday_fragment page. Create these objects in the monday_list xml file. Create a Monday_list_fragment that extends the ListFragment class. In the onCreateView() method inflate() the monday_list resource.
In the onCreate() method you need to provide a single array of strings as the source for the list in the setListAdapter() method used to fill the ListView on the page. The strings are in your static ListArray variable in MainActivity. However, they are not in the form required and you must unpack the diaryLog elements to get to the required strings. To do this you must create a String array big enough to hold the strings to display. As this consists of a date/time string followed by a diary entry string there will be two such strings for each diaryLog element. Use a Iterator to iterate through your ListArray. Copy the date string and diary string strings into your local string. Then use this local string as the relevant parameter of setListAdapter() so that the ListView displays the required strings.
Question:
How to get only the strings from the custom arraylist and make a string array using the strings. So later those can be inflated to a listview. Unless my complete approach is wrong. Thanks in advance.
My class and its's constructor
public DiaryLogs(int day, String timeEntry, String entryEntered) {
super();
this.day = day;
this.timeEntry = timeEntry;
this.entryEntered = entryEntered;
}
Static Variables in MainActivity.java
public static int Monday=0;
public static int Tuesday=1;
public static int Wednesday=2;
public static int Thursday=3;
public static int Friday=4;
public static String timeEntry;
public static String entryEntered;
public static ArrayList<DiaryLogs> logs;
public static String[] entry;
OnClick method in Monday_fragment.java
public void onClick(View v) {
// TODO Auto-generated method stub
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();
DiaryLogs dLogs = new DiaryLogs(MainActivity.Monday, timeEntry, entryEntered);
MainActivity.logs = new ArrayList<DiaryLogs>();
MainActivity.logs.add(dLogs);
UPDATE
in Monday_fragment's onCreate method when using the iterator i can see the value, but in Monday_listz_fragment's its giving runtime error.
Class Monday_list_fragment:
public class Monday_list_fragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater
.inflate(R.layout.monday_list_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// setListAdapter(new ArrayAdapter<String>(getActivity(),
// android.R.layout.simple_list_item_1, data ));
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Iterator it = MainActivity.logs.iterator();
while (it.hasNext()) {
Object element = it.next();
for (int j = 0; j < MainActivity.logs.size(); j++) {
String[] text = { MainActivity.logs.get(j).getTimeEntry(),
MainActivity.logs.get(j).getEntryEntered() };
System.out.println(text[0] + "\n" + text[1]);
}
}
super.onCreate(savedInstanceState);
}
}
The purpose is to get a string array of the strings {MainActivity.logs.get(j).getTimeEntry(),MainActivity.logs.get(j).getEntryEntered()}
so i can use it as a data/array source for setListadapter.
LOGCAT Error:
04-11 23:45:53.992: E/AndroidRuntime(10006): FATAL EXCEPTION: main
04-11 23:45:53.992: E/AndroidRuntime(10006): Process: com.example.s0217980_diary, PID: 10006
04-11 23:45:53.992: E/AndroidRuntime(10006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s0217980_diary/com.example.s0217980_diary.MainActivity}: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.os.Looper.loop(Looper.java:136)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 23:45:53.992: E/AndroidRuntime(10006): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 23:45:53.992: E/AndroidRuntime(10006): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 23:45:53.992: E/AndroidRuntime(10006): at dalvik.system.NativeStart.main(Native Method)
04-11 23:45:53.992: E/AndroidRuntime(10006): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Activity.setContentView(Activity.java:1929)
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.example.s0217980_diary.MainActivity.onCreate(MainActivity.java:52)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Activity.performCreate(Activity.java:5231)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 23:45:53.992: E/AndroidRuntime(10006): ... 11 more
04-11 23:45:53.992: E/AndroidRuntime(10006): Caused by: java.lang.NullPointerException
04-11 23:45:53.992: E/AndroidRuntime(10006): at com.example.s0217980_diary.Monday_list_fragment.onCreate(Monday_list_fragment.java:45)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Fragment.performCreate(Fragment.java:1678)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.app.Activity.onCreateView(Activity.java:4786)
04-11 23:45:53.992: E/AndroidRuntime(10006): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-11 23:45:53.992: E/AndroidRuntime(10006): ... 21 more
04-11 23:48:11.294: D/dalvikvm(10069): GC_FOR_ALLOC freed 71K, 6% free 3267K/3448K, paused 16ms, total 24ms
04-11 23:48:11.364: D/AndroidRuntime(10069): Shutting down VM
04-11 23:48:11.364: W/dalvikvm(10069): threadid=1: thread exiting with uncaught exception (group=0xb0c9db20)
04-11 23:48:11.374: E/AndroidRuntime(10069): FATAL EXCEPTION: main
04-11 23:48:11.374: E/AndroidRuntime(10069): Process: com.example.s0217980_diary, PID: 10069
04-11 23:48:11.374: E/AndroidRuntime(10069): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s0217980_diary/com.example.s0217980_diary.MainActivity}: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.os.Looper.loop(Looper.java:136)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 23:48:11.374: E/AndroidRuntime(10069): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 23:48:11.374: E/AndroidRuntime(10069): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 23:48:11.374: E/AndroidRuntime(10069): at dalvik.system.NativeStart.main(Native Method)
04-11 23:48:11.374: E/AndroidRuntime(10069): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class fragment
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Activity.setContentView(Activity.java:1929)
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.example.s0217980_diary.MainActivity.onCreate(MainActivity.java:52)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Activity.performCreate(Activity.java:5231)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 23:48:11.374: E/AndroidRuntime(10069): ... 11 more
04-11 23:48:11.374: E/AndroidRuntime(10069): Caused by: java.lang.NullPointerException
04-11 23:48:11.374: E/AndroidRuntime(10069): at com.example.s0217980_diary.Monday_list_fragment.onCreate(Monday_list_fragment.java:45)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Fragment.performCreate(Fragment.java:1678)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.app.Activity.onCreateView(Activity.java:4786)
04-11 23:48:11.374: E/AndroidRuntime(10069): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-11 23:48:11.374: E/AndroidRuntime(10069): ... 21 more