· 9 years ago · Dec 21, 2016, 06:58 PM
1SQLiteDatabase winlife;
2EditText edt_meta;
3EditText edt_tempo;
4EditText edt_dataExpiracao;
5
6/**
7 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
8 */
9private NavigationDrawerFragment mNavigationDrawerFragment;
10
11/**
12 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
13 */
14private CharSequence mTitle;
15
16
17
18
19@Override
20protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_listagem);
23
24 mNavigationDrawerFragment = (NavigationDrawerFragment)
25 getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
26 mTitle = getTitle();
27
28 // Set up the drawer.
29 mNavigationDrawerFragment.setUp(
30 R.id.navigation_drawer,
31 (DrawerLayout) findViewById(R.id.drawer_layout));
32
33 winlife = openOrCreateDatabase( "winlife.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
34 winlife.setVersion(3);
35 winlife.setLocale(Locale.getDefault());
36
37 String criaTabelaMetas = "CREATE TABLE IF NOT EXISTS metas (id INTEGER PRIMARY KEY AUTOINCREMENT, descricao TEXT, tempoRealizacao INTEGER, dataExpiracao INTEGER)";
38 winlife.execSQL(criaTabelaMetas);
39
40 edt_meta = (EditText) findViewById(R.id.edt_meta);
41 edt_tempo = (EditText) findViewById(R.id.edt_tempo);
42 edt_dataExpiracao = (EditText) findViewById(R.id.edt_dataExpiracao);
43
44
45 Button bt_salvar = (Button) findViewById(R.id.bt_salvar);
46 bt_salvar.setOnClickListener(this);
47
48 Cursor cur = winlife.rawQuery("SELECT EXISTS (SELECT 1 FROM metas)", null);
49
50 if (cur != null) {
51 cur.moveToFirst();
52 if (cur.getInt (0) == 0) {
53 Intent it = new Intent(this,PrimeiraActivity.class);
54 startActivity(it);
55 finish();
56 } else {
57 // Tabela ja contem dados.
58 }
59 }
60}
61
62
63@Override
64public void onClick(View v) {
65 if (v.getId()== R.id.bt_salvar) {
66 String nomeMeta = edt_meta.getText().toString();
67 //String tempoMeta = edt_tempo.getText().toString();
68
69 ContentValues values = new ContentValues();
70 values.put("descricao", nomeMeta);
71 //values.put("tempoRealizacao", tempoMeta);
72
73 winlife.insert("metas", "", values);
74 }
75}
76
77
78
79@Override
80public void onNavigationDrawerItemSelected(int position) {
81 // update the main content by replacing fragments
82 FragmentManager fragmentManager = getSupportFragmentManager();
83 fragmentManager.beginTransaction()
84 .replace(R.id.container, placeholderFragment.newInstance(position + 1))
85 .commit();
86 }
87
88public void onSectionAttached(int number) {
89 switch (number) {
90 case 1:
91 mTitle = getString(R.string.title_section1);
92 break;
93 case 2:
94 mTitle = getString(R.string.title_section2);
95 break;
96 case 3:
97 mTitle = getString(R.string.title_section3);
98 break;
99 }
100}
101
102public void restoreActionBar() {
103 ActionBar actionBar = getSupportActionBar();
104 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
105 actionBar.setDisplayShowTitleEnabled(true);
106 actionBar.setTitle(mTitle);
107}
108
109
110@Override
111public boolean onCreateOptionsMenu(Menu menu) {
112 if (!mNavigationDrawerFragment.isDrawerOpen()) {
113 // Only show items in the action bar relevant to this screen
114 // if the drawer is not showing. Otherwise, let the drawer
115 // decide what to show in the action bar.
116 getMenuInflater().inflate(R.menu.listagem, menu);
117 restoreActionBar();
118 return true;
119 }
120 return super.onCreateOptionsMenu(menu);
121}
122
123@Override
124public boolean onOptionsItemSelected(MenuItem item) {
125 // Handle action bar item clicks here. The action bar will
126 // automatically handle clicks on the Home/Up button, so long
127 // as you specify a parent activity in AndroidManifest.xml.
128 int id = item.getItemId();
129
130 //noinspection SimplifiableIfStatement
131 if (id == R.id.action_settings) {
132 return true;
133 }
134
135 return super.onOptionsItemSelected(item);
136}
137
138
139
140/**
141 * A placeholder fragment containing a simple view.
142 */
143public static class PlaceholderFragment extends Fragment {
144 /**
145 * The fragment argument representing the section number for this
146 * fragment.
147 */
148 private static final String ARG_SECTION_NUMBER = "section_number";
149
150 /**
151 * Returns a new instance of this fragment for the given section
152 * number.
153 */
154 public static PlaceholderFragment newInstance(int sectionNumber) {
155 PlaceholderFragment fragment = new PlaceholderFragment();
156 Bundle args = new Bundle();
157 args.putInt(ARG_SECTION_NUMBER, sectionNumber);
158 fragment.setArguments(args);
159 return fragment;
160 }
161
162 public PlaceholderFragment() {
163 }
164
165
166
167 @Override
168 public void onAttach(Activity activity) {
169 super.onAttach(activity);
170 ((ListagemActivity) activity).onSectionAttached(
171 getArguments().getInt(ARG_SECTION_NUMBER));
172 }
173}