Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package com.myproject.fla;
  2.  
  3. import android.app.Activity;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteException;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.widget.ListView;
  11. import android.widget.SimpleCursorAdapter;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends Activity {
  15.  
  16. private SQLiteDatabase db;
  17. private Cursor cursor;
  18. CustomCursorAdapter listAdapter;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. SQLiteOpenHelper databaseHelper = new DatabaseHelper(this);
  26. ListView listView = (ListView) findViewById(R.id.listView_records);
  27.  
  28. try {
  29.  
  30. db = databaseHelper.getReadableDatabase();
  31.  
  32.  
  33. cursor = db.query("record", new String[]{"KEY_ID", "KEY_DATE", "KEY_TIME", "KEY_STAFF", "KEY_ANIMAL", "KEY_ACTIVITY", "KEY_DETAIL"}, null, null, null, null, null);
  34.  
  35. //cursor = db.query("RECORD", new String[]{"_id", "NAME", "TYPE", "DESCRIPTION"}, null, null, null, null, "_id DESC");
  36. //cursor = db.query("RECORD", new String[]{"_id", "NAME", "TYPE", "DESCRIPTION"}, null, null, null, null, "NAME ASC");
  37.  
  38. //cursor = db.query("RECORD", new String[]{"_id", "NAME", "TYPE", "DESCRIPTION"}, "TYPE = ?", new String[] {"Droid"}, null, null, null);
  39. //cursor = db.query("RECORD", new String[]{"_id", "NAME", "TYPE", "DESCRIPTION"}, "TYPE = ? OR TYPE = ?", new String[] {"Droid", "Location"}, null, null, null);
  40. //cursor = db.query("RECORD", new String[]{"_id", "NAME", "TYPE", "DESCRIPTION"}, "_id = ?", new String[] {Integer.toString(3)}, null, null, null);
  41.  
  42. listAdapter = new CustomCursorAdapter(this, cursor, 0);
  43.  
  44. listView.setAdapter(listAdapter);
  45.  
  46. } catch(SQLiteException e) {
  47. Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT).show();
  48. }
  49.  
  50. //Log.i(">>> Activity_main", "Cursor size=" + Integer.toString(cursor.getCount()));
  51. Log.i(">>> Activity_main", "Database records:" + DatabaseHelper.getDatabaseContentsAsString(db));
  52. }
  53.  
  54. @Override
  55. protected void onDestroy() {
  56. super.onDestroy();
  57.  
  58. if(cursor != null) cursor.close();
  59. if(db != null) db.close();
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement