Guest User

Untitled

a guest
Oct 19th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. public class CategorieActivity extends AppCompatActivity {
  2. Toolbar toolbar;
  3. ListView listview;
  4.  
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_categorie);
  10. toolbar= (Toolbar) findViewById(R.id.toolbar);
  11. toolbar.setTitle(getResources().getString(R.string.app_name));
  12. listview=(ListView)findViewById(R.id.listView);
  13. //DBConnections db=new DBConnections(this);
  14.  
  15. DBConnections myDbHelper = new DBConnections(this);
  16. try {
  17. myDbHelper.createDatabase();
  18.  
  19. } catch (IOException ioe) {
  20.  
  21. throw new Error("Unable to create database");
  22. }
  23.  
  24. try {
  25. myDbHelper.openDatabase();
  26.  
  27. }catch(SQLException sqle){
  28.  
  29. throw sqle;
  30. }
  31. ArrayList arrayliste=myDbHelper.getAllrecord();
  32.  
  33.  
  34. ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(CategorieActivity.this,android.R.layout.simple_list_item_1,arrayliste);
  35. //<String> myAdapter=new ArrayAdapter<String>(CategorieActivity.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.categorie));
  36. listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  37. @Override
  38. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  39. Intent myIntent=new Intent(CategorieActivity.this,ListSatutsActivity.class);
  40. myIntent.putExtra("Categorie",listview.getItemAtPosition(position).toString());
  41. startActivity(myIntent);
  42. }
  43. });
  44. listview.setAdapter(myAdapter);
  45. }
  46. }
  47.  
  48. public class DBConnections extends SQLiteOpenHelper {
  49. public static final String DBName="LastusStauts.db";
  50. public static final int version=1;
  51. public final static String DATABASE_PATH = "/data/data/com.example.hp.latestsatuts/databases/";
  52. private final Context myContext;
  53. private SQLiteDatabase myDataBase;
  54. public DBConnections(Context context) {
  55.  
  56. super(context, DBName,null, version);
  57.  
  58. this.myContext=context;
  59. Log.v("Error","Appel au constructeur");
  60. }
  61. public void createDatabase() throws IOException
  62. {
  63.  
  64. boolean dbExist = checkDataBase();
  65.  
  66. if(dbExist)
  67. {
  68. Log.v("DB Exists", "db exists");
  69. // By calling this method here onUpgrade will be called on a
  70. // writeable database, but only if the version number has been
  71. // bumped
  72. //onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
  73. }
  74.  
  75. boolean dbExist1 = checkDataBase();
  76. if(!dbExist1)
  77. {
  78. this.getReadableDatabase();
  79. try
  80. {
  81. this.close();
  82. copyDataBase();
  83. }
  84. catch (IOException e)
  85. {
  86. throw new Error("Error copying database");
  87. }
  88. }
  89.  
  90. }
  91. //Check database already exist or not
  92. private boolean checkDataBase()
  93. {
  94. boolean checkDB = false;
  95. try
  96. {
  97. String myPath = DATABASE_PATH + DBName;
  98. File dbfile = new File(myPath);
  99. checkDB = dbfile.exists();
  100. }
  101. catch(SQLiteException e)
  102. {
  103. }
  104. return checkDB;
  105. }
  106. //Copies your database from your local assets-folder to the just created empty database in the system folder
  107. private void copyDataBase() throws IOException
  108. {
  109.  
  110. InputStream mInput = myContext.getAssets().open(DBName);
  111. String outFileName = DATABASE_PATH + DBName;
  112. OutputStream mOutput = new FileOutputStream(outFileName);
  113. byte[] mBuffer = new byte[2024];
  114. int mLength;
  115. while ((mLength = mInput.read(mBuffer)) > 0) {
  116. mOutput.write(mBuffer, 0, mLength);
  117. }
  118. mOutput.flush();
  119. mOutput.close();
  120. mInput.close();
  121. }
  122. //delete database
  123. public void db_delete()
  124. {
  125. File file = new File(DATABASE_PATH + DBName);
  126. if(file.exists())
  127. {
  128. file.delete();
  129. System.out.println("delete database file.");
  130. }
  131. }
  132. //Open database
  133. public void openDatabase() throws SQLException
  134. {
  135. String myPath = DATABASE_PATH + DBName;
  136. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  137. }
  138.  
  139. public synchronized void closeDataBase()throws SQLException
  140. {
  141. if(myDataBase != null)
  142. myDataBase.close();
  143. super.close();
  144. }
  145.  
  146. @Override
  147. public void onCreate(SQLiteDatabase myDataBase) {
  148.  
  149. }
  150.  
  151. @Override
  152. public void onUpgrade(SQLiteDatabase myDataBase, int oldVersion, int newVersion) {
  153.  
  154. }
  155. public ArrayList getAllrecord(){
  156.  
  157. ArrayList arraylist=new ArrayList();
  158. // Pour lire a partir de base de donnees
  159. SQLiteDatabase db=this.getReadableDatabase();
  160. //Cursor notre structure pour deplacer
  161.  
  162. Cursor res =myDataBase.rawQuery("select * from Categories",null);
  163. res.moveToFirst();
  164. while(res.isAfterLast()==false){
  165. arraylist.add(res.getString(res.getColumnIndex("Categorie")));
  166. res.moveToNext();
  167. }
  168. return arraylist;
  169. }
  170. }
Add Comment
Please, Sign In to add comment