Back to project page ProjectStudio.
The source code is released under:
Apache License
If you think the Android project ProjectStudio listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** *//from ww w . j a va 2 s . co m */ package fragments; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import com.example.uniutilproject.R; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import DB_Provider.TaskDataSource; import adapters.TaskCardAdapter; /** * @author desmond */ public class HomeFragment extends Fragment { private static final int HIDE_SETTINGS_ICON = 1; private DrawerLayout parent_drawer; private TextView date_text; private Date date_format; private TextView home_title; private ListView home_list; private TaskDataSource dataSource; private int menu_settings_state; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.home_fragment, container, false); /* //REMOVE THE SETTINGS ICON FROM THE ACTIONBAR menu_settings_state = HIDE_SETTINGS_ICON; //now call onCreteOptionsMenu again getActivity().invalidateOptionsMenu(); */ //CALL DATABASE dataSource = new TaskDataSource(getActivity()); try { dataSource.open(); } catch (SQLException e) { e.printStackTrace(); } //IF DRAWER HAS BEEN DISABLED BY SOME OTHER FRAGMENT //ENABLE DRAWER parent_drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout); if (parent_drawer.getDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) == 0){ parent_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); } //GET CURRENT DAY getCurrentDate(); String todays_date = new SimpleDateFormat("EEEE, d MMMM").format(date_format); final String today = new SimpleDateFormat("EEEE").format(date_format); //INFLATE VIEWS HERE if (view != null) { home_list = (ListView) view.findViewById(R.id.home_listview); date_text = (TextView) view.findViewById(R.id.todays_date); home_title = (TextView) view.findViewById(R.id.home_title); } date_text.setText(todays_date); new android.os.Handler().post(new Runnable() { @Override public void run() { Cursor cursor = dataSource.getTaskByDay(today); //check if cursor is empty if (cursor.getCount() == 0) { home_title.setText("NO TASKS FOR TODAY"); } TaskCardAdapter adapter = new TaskCardAdapter(getActivity(), cursor, true); home_list.setAdapter(adapter); adapter.notifyDataSetChanged(); } }); dataSource.close(); return view; } //SET CURRENT DATE public void getCurrentDate(){ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); try { date_format = dateFormat.parse(year + "-" + (month + 1) + "-" + day); } catch (ParseException e) { e.printStackTrace(); } } @Override public void onResume() { try { dataSource.open(); } catch (SQLException e) { e.printStackTrace(); } super.onResume(); } @Override public void onPause() { dataSource.close(); super.onPause(); } // Register the fragment as a contributor to the Options Menu // call setHasOptions within its onCreate Handler @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } // add a menu item when this fragment is called @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); // custom menu (action bar layout) inflater.inflate(R.menu.task_menu, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); // Find which menu item has been selected switch (item.getItemId()) { // Check for each known menu item case R.id.task_menu_addbtn: showChildFragment(); return true; // Return false if the menu item has not been handled default: return false; } } // private void showChildFragment() { FragmentManager fm = getFragmentManager(); AddTaskFragment frag = new AddTaskFragment(); FragmentTransaction transaction = fm.beginTransaction(); // For a little polish, specify a transition animation transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit(); } //REMOVE THE THREE DOT ICON FROM THE ACTIONBAR, SINCE IT IS NOT NEEDED FOR NOW @Override public void onPrepareOptionsMenu(Menu menu) { MenuItem item = menu.findItem(R.id.action_settings); item.setVisible(false); super.onPrepareOptionsMenu(menu); } }