Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. public class MainActivity extends Activity implements OnClickListener {
  2.  
  3. private DrawerLayout mDrawerLayout;
  4. private ListView mDrawerList;
  5. private ActionBarDrawerToggle mDrawerToggle;
  6.  
  7. // nav drawer title
  8. private CharSequence mDrawerTitle;
  9.  
  10. // used to store app title
  11. private CharSequence mTitle;
  12.  
  13. // slide menu items
  14. private String[] navMenuTitles;
  15. // private TypedArray navMenuIcons;
  16.  
  17. private ArrayList<NavDrawerItem> navDrawerItems;
  18. private NavDrawerListAdapter adapter;
  19.  
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. try {
  25. tap_on_plus = (ImageView) findViewById(R.id.imageNewNote);
  26. Contact_listview = (ListView) findViewById(R.id.listView1);
  27. findViewById(R.id.button1).setOnClickListener(this);
  28. findViewById(R.id.button2).setOnClickListener(this);
  29. Set_Referash_Data();
  30.  
  31. } catch (Exception e) {
  32. // TODO: handle exception
  33. Log.e("some error", "" + e);
  34. }
  35.  
  36. // tikras folderis "/Android/data/com.strategeens"
  37. File direct = new File(Environment.getExternalStorageDirectory()
  38. + "/BackupFolder");
  39.  
  40. if (!direct.exists()) {
  41. if (direct.mkdir()) {
  42. // directory is created;
  43. }
  44. }
  45.  
  46.  
  47. /////////////naujibundle///////////
  48.  
  49. mTitle = mDrawerTitle = getTitle();
  50.  
  51. // load slide menu items
  52. navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
  53.  
  54. // nav drawer icons from resources
  55. // navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
  56.  
  57. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  58. mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
  59.  
  60. navDrawerItems = new ArrayList<NavDrawerItem>();
  61.  
  62. // adding nav drawer items to array
  63. // Home
  64. navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], 1/*navMenuIcons.getResourceId(0, -1)*/));
  65. // Find People
  66. navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], 1/*navMenuIcons.getResourceId(1, -1)*/));
  67.  
  68. // Recycle the typed array
  69. // navMenuIcons.recycle();
  70.  
  71. mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
  72.  
  73. // setting the nav drawer list adapter
  74. adapter = new NavDrawerListAdapter(getApplicationContext(),
  75. navDrawerItems);
  76. mDrawerList.setAdapter(adapter);
  77.  
  78. // enabling action bar app icon and behaving it as toggle button
  79. getActionBar().setDisplayHomeAsUpEnabled(true);
  80. getActionBar().setHomeButtonEnabled(true);
  81.  
  82. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
  83. R.drawable.ic_drawer, //nav menu toggle icon
  84. R.string.app_name, // nav drawer open - description for accessibility
  85. R.string.app_name // nav drawer close - description for accessibility
  86. ) {
  87. public void onDrawerClosed(View view) {
  88. getActionBar().setTitle(mTitle);
  89. // calling onPrepareOptionsMenu() to show action bar icons
  90. invalidateOptionsMenu();
  91. }
  92.  
  93. public void onDrawerOpened(View drawerView) {
  94. getActionBar().setTitle(mDrawerTitle);
  95. // calling onPrepareOptionsMenu() to hide action bar icons
  96. invalidateOptionsMenu();
  97. }
  98. };
  99. mDrawerLayout.setDrawerListener(mDrawerToggle);
  100. }
  101.  
  102. /**
  103. * Slide menu item click listener
  104. * */
  105. public class SlideMenuClickListener implements
  106. ListView.OnItemClickListener {
  107. @Override
  108. public void onItemClick(AdapterView<?> parent, View view, int position,
  109. long id) {
  110. // display view for selected nav drawer item
  111. switch (position) {
  112. case 0:
  113. importDB();
  114. System.out.println("AAAAAAAAAAAAAAAAAAA1" + position);
  115. break;
  116. case 1:
  117. System.out.println("AAAAAAAAAAAAAAAAAAA2" + position);
  118. exportDB();
  119. break;
  120. }
  121. // update selected item and title, then close the drawer
  122. mDrawerList.setItemChecked(position, true);
  123. mDrawerList.setSelection(position);
  124. setTitle(navMenuTitles[position]);
  125. mDrawerLayout.closeDrawer(mDrawerList);
  126. }
  127. }
  128.  
  129. @Override
  130. public void setTitle(CharSequence title) {
  131. mTitle = title;
  132. getActionBar().setTitle(mTitle);
  133. }
  134.  
  135. /**
  136. * When using the ActionBarDrawerToggle, you must call it during
  137. * onPostCreate() and onConfigurationChanged()...
  138. */
  139.  
  140. @Override
  141. protected void onPostCreate(Bundle savedInstanceState) {
  142. super.onPostCreate(savedInstanceState);
  143. // Sync the toggle state after onRestoreInstanceState has occurred.
  144. mDrawerToggle.syncState();
  145. }
  146.  
  147. @Override
  148. public void onConfigurationChanged(Configuration newConfig) {
  149. super.onConfigurationChanged(newConfig);
  150. // Pass any configuration change to the drawer toggls
  151. mDrawerToggle.onConfigurationChanged(newConfig);
  152. }
  153. /////////////////////visas kitas sudas///////////pabaiga
  154.  
  155. // importing database
  156. private void importDB() {
  157. // TODO Auto-generated method stub
  158.  
  159. try {
  160. File sd = Environment.getExternalStorageDirectory();
  161. File data = Environment.getDataDirectory();
  162.  
  163. if (sd.canWrite()) {
  164. String currentDBPath = "//data//" + "com.strategeens.drugnotes"
  165. + "//databases//" + "drugsManager";
  166. String backupDBPath = "/BackupFolder/drugsManager";
  167. File backupDB = new File(data, currentDBPath);
  168. File currentDB = new File(sd, backupDBPath);
  169.  
  170. FileChannel src = new FileInputStream(currentDB).getChannel();
  171. FileChannel dst = new FileOutputStream(backupDB).getChannel();
  172. dst.transferFrom(src, 0, src.size());
  173. src.close();
  174. dst.close();
  175. Set_Referash_Data();
  176. cAdapter.notifyDataSetChanged();
  177. Toast.makeText(getBaseContext(), backupDB.toString(),
  178. Toast.LENGTH_LONG).show();
  179.  
  180. }
  181. } catch (Exception e) {
  182.  
  183. Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
  184. .show();
  185.  
  186. }
  187. }
  188.  
  189. // exporting database
  190. private void exportDB() {
  191. // TODO Auto-generated method stub
  192.  
  193. try {
  194. File sd = Environment.getExternalStorageDirectory();
  195. File data = Environment.getDataDirectory();
  196.  
  197. if (sd.canWrite()) {
  198. String currentDBPath = "//data//" + "com.strategeens.drugnotes"
  199. + "//databases//" + "drugsManager";
  200. String backupDBPath = "/BackupFolder/drugsManager";
  201. File currentDB = new File(data, currentDBPath);
  202. File backupDB = new File(sd, backupDBPath);
  203.  
  204. FileChannel src = new FileInputStream(currentDB).getChannel();
  205. FileChannel dst = new FileOutputStream(backupDB).getChannel();
  206. dst.transferFrom(src, 0, src.size());
  207. src.close();
  208. dst.close();
  209. Toast.makeText(getBaseContext(), backupDB.toString(),
  210. Toast.LENGTH_LONG).show();
  211.  
  212. }
  213. } catch (Exception e) {
  214.  
  215. Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
  216. .show();
  217. }
  218. }
  219.  
  220. @Override
  221. public boolean onCreateOptionsMenu(Menu menu) {
  222. // Inflate the menu; this adds items to the action bar if it is present.
  223. getMenuInflater().inflate(R.menu.mainmenu, menu);
  224. return true;
  225. }
  226.  
  227. @Override
  228. public boolean onOptionsItemSelected(MenuItem item) {
  229.  
  230. // toggle nav drawer on selecting action bar app icon/title
  231. if (mDrawerToggle.onOptionsItemSelected(item)) {
  232. return true;
  233. }
  234. // Handle action bar actions click
  235. switch (item.getItemId()) {
  236. case R.id.action_settings:
  237. return true;
  238. // default:
  239. // return super.onOptionsItemSelected(item);
  240. }
  241.  
  242. super.onOptionsItemSelected(item);
  243. switch (item.getItemId()) {
  244. case R.id.item1:
  245.  
  246. // TODO Auto-generated method stub
  247. Intent add_user = new Intent(MainActivity.this, AddUpdateDrug.class);
  248. add_user.putExtra("called", "add");
  249. add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
  250. | Intent.FLAG_ACTIVITY_NEW_TASK);
  251. startActivity(add_user);
  252. finish();
  253.  
  254. }
  255. return false;
  256. }
  257. }
  258.  
  259. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  260. xmlns:tools="http://schemas.android.com/tools"
  261. android:layout_width="match_parent"
  262. android:layout_height="match_parent"
  263. android:id="@+id/drawer_layout" >
  264.  
  265. <!-- Framelayout to display Fragments -->
  266. <FrameLayout
  267. android:id="@+id/frame_container"
  268. android:layout_width="match_parent"
  269. android:layout_height="match_parent" />
  270.  
  271. <!-- Listview to display slider menu -->
  272. <ListView
  273. android:id="@+id/list_slidermenu"
  274. android:layout_width="240dp"
  275. android:layout_height="match_parent"
  276. android:layout_gravity="start"
  277. android:choiceMode="singleChoice"
  278. android:divider="@color/list_divider"
  279. android:dividerHeight="1dp"
  280. android:listSelector="@drawable/list_selector"
  281. android:background="@color/list_background"/>
  282.  
  283. <RelativeLayout
  284. android:layout_width="match_parent"
  285. android:layout_height="match_parent"
  286. android:paddingBottom="@dimen/activity_vertical_margin"
  287. android:paddingLeft="@dimen/activity_horizontal_margin"
  288. android:paddingRight="@dimen/activity_horizontal_margin"
  289. android:paddingTop="@dimen/activity_vertical_margin"
  290. tools:context=".MainActivity" >
  291.  
  292. <ListView
  293. android:id="@+id/listView1"
  294. android:layout_width="match_parent"
  295. android:layout_height="wrap_content">
  296. </ListView>
  297.  
  298. <ImageView
  299. android:id="@+id/imageNewNote"
  300. android:layout_width="250dp"
  301. android:layout_height="230dp"
  302. android:layout_centerHorizontal="true"
  303. android:layout_centerVertical="true"
  304. android:src="@drawable/tap_on_a_plus"
  305. android:contentDescription="@string/newNote" />
  306.  
  307. <Button
  308. android:id="@+id/button2"
  309. android:layout_width="wrap_content"
  310. android:layout_height="wrap_content"
  311. android:layout_alignLeft="@+id/imageNewNote"
  312. android:layout_alignParentBottom="true"
  313. android:layout_marginLeft="51dp"
  314. android:text="Export" />
  315.  
  316. <Button
  317. android:id="@+id/button1"
  318. android:layout_width="wrap_content"
  319. android:layout_height="wrap_content"
  320. android:layout_alignParentBottom="true"
  321. android:layout_toRightOf="@+id/button2"
  322. android:text="Import" />
  323.  
  324. </RelativeLayout>
  325. </android.support.v4.widget.DrawerLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement