Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. package com.example.listdisplay;
  2.  
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.ListView;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import java.io.File;
  13. import java.io.StringReader;
  14. import java.util.ArrayList;
  15.  
  16. public class ListDisplay extends Activity {
  17. // Array of strings...
  18. private String[] ABA = {"1","1","1","1","1","1"};
  19.  
  20. private SQLiteDatabase db2;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26.  
  27. // Cursor cur
  28. ArrayAdapter adapter = new ArrayAdapter<String>(this,
  29. R.layout.activity_listview, ABA);
  30. TextView texttest = (TextView) findViewById(R.id.texttest);
  31. ListView listView = (ListView) findViewById(R.id.mobile_list);
  32. listView.setAdapter(adapter);
  33.  
  34. opendb();
  35. insert();
  36. // show();
  37.  
  38. /*String sql = "select * from AddressBookTable";
  39. Cursor c1 = db2.rawQuery(sql, null);
  40. c1.moveToPosition(-1);
  41. do{
  42. texttest.setText(c1.getString(0));
  43. }
  44. while (c1.moveToNext() );
  45. c1.close();*/
  46. }
  47. /* private void show(){
  48. String name;
  49. int phone;
  50. String email;
  51. try {
  52. // for (int i =0;i<mobileArray.size();i++){
  53. String sql = "select * from AddressBookTable";
  54. Cursor c1 = db2.rawQuery(sql, null);
  55. if (c1.getCount() == 0) {
  56. Toast.makeText(this, "add data", Toast.LENGTH_LONG).show();
  57. } else {
  58.  
  59. for (int i=0;i<ABA.length;i++){
  60.  
  61. c1.moveToPosition(-1);
  62.  
  63. do {
  64.  
  65. name = c1.getString(0);
  66. phone = c1.getInt(1);
  67. email = c1.getString(2);
  68. String phone2 = String.valueOf(phone);
  69.  
  70.  
  71. ABA[i]=name+"n"+phone2+"n"+email;
  72. } while (c1.moveToNext() && ABA.length<6);
  73. }
  74.  
  75. c1.close();
  76. // mobileArray[i].concat(name).concat(phone2).concat(email);
  77.  
  78. // }
  79. }
  80. }
  81. catch (Exception e){
  82. Toast.makeText(this,"data couldnt display ",Toast.LENGTH_LONG).show();
  83.  
  84. }
  85. }*/
  86.  
  87. private void opendb() {
  88. File storagePath = getApplication().getFilesDir();
  89. String mydbPath = storagePath + "/" + "ABD";
  90. try {
  91.  
  92. db2 = SQLiteDatabase.openDatabase(mydbPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
  93. Toast.makeText(this, "DB Created", Toast.LENGTH_LONG).show();
  94.  
  95. } catch (Exception e) {
  96. Toast.makeText(this, "Failed creating db", Toast.LENGTH_LONG).show();
  97. finish();
  98. }
  99. }
  100.  
  101. private void insert() {
  102.  
  103. db2.beginTransaction();
  104. try {
  105. db2.execSQL("create table AddressBookTable(" +
  106. "name text," +
  107. " phone integer, " +
  108. "email text ); ");
  109. db2.setTransactionSuccessful();
  110. Toast.makeText(this, "table created", Toast.LENGTH_LONG).show();
  111.  
  112.  
  113. } catch (Exception e) {
  114. Toast.makeText(this, "failed creating table ", Toast.LENGTH_LONG).show();
  115. finish();
  116. } finally {
  117. db2.endTransaction();
  118. }
  119.  
  120. //insertion
  121. db2.beginTransaction();
  122. try {
  123. db2.execSQL("insert into AddressBookTable(name,phone,email)" +
  124. "values ('Abdullah','393939399','abdw2@gmail.com')");
  125. db2.execSQL("insert into AddressBookTable(name,phone,email)" +
  126. "values ('norah','058389111','no3021@gmail.com')");
  127. db2.execSQL("insert into AddressBookTable(name,phone,email)" +
  128. "values ('Suliman','043911933','suliman766@gmail.com')");
  129. db2.execSQL("insert into AddressBookTable(name,phone,email)" +
  130. "values ('rana','058313466','rrr-1@gmail.com')");
  131. db2.execSQL("insert into AddressBookTable(name,phone,email)" +
  132. "values ('khalid','038828822','kk291@gmail.com')");
  133. db2.execSQL("insert into AddressBookTable(name,phone,email)" +
  134. "values ('Yazeed','038828822','kk291@gmail.com')");
  135.  
  136. db2.setTransactionSuccessful();
  137. Toast.makeText(this, "row inserted ", Toast.LENGTH_LONG).show();
  138.  
  139. } catch (Exception e) {
  140. Toast.makeText(this, "failed inserting ", Toast.LENGTH_LONG).show();
  141.  
  142. } finally {
  143. db2.endTransaction();
  144. }
  145.  
  146.  
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement