Guest User

Untitled

a guest
Jan 14th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. Cannot retrieve from second table in sqlite database and display in listview
  2. //User Table
  3.  
  4. public static final String KEY_ROWID = "_id";
  5. public static final String KEY_UNAME = "name";
  6. public static final String KEY_USURNAME = "surname";
  7. public static final String KEY_UUSERNAME = "username";
  8. public static final String KEY_UPASSWORD = "password";
  9. public static final String KEY_UEMAILADDRESS = "emailadd";
  10.  
  11. //Account Table
  12. public static final String KEY_ROWID1 = "_id1";
  13. public static final String KEY_BANKNAME =" bankname";
  14. public static final String KEY_TYPE = " type";
  15. public static final String KEY_ACCNUM = " accnum";
  16. public static final String KEY_BALANCE = " balance";
  17. public static final String KEY_EXPIRYDATE = " expirydate";
  18.  
  19. //Database
  20. private static final String DATABASE_NAME = "MMS2";
  21. public static final String DATABASE_TABLE = " Usertb";
  22. public static final String DATABASE_TABLE1 = " Accounttb";
  23. private static final String DATABASE_TABLE2 = " Transactiontb";
  24. private static final String DATABASE_TABLE3 = " Categorytb";
  25.  
  26. //Database Version
  27. private static final int DATABASE_VERSION = 1;
  28. private static String Usertb;
  29. private static String Accounttb;
  30.  
  31. private DbHelper MHelper;
  32. private final Context MContext;
  33. private SQLiteDatabase Mdatabase;
  34.  
  35. private static final String DATABASE_USER_TABLE = "CREATE TABLE" + DATABASE_TABLE + " (" +
  36. KEY_ROWID + " INTEGER PRIMARY KEY, " +
  37. KEY_UNAME + " TEXT , " +
  38. KEY_USURNAME + " TEXT , " +
  39. KEY_UUSERNAME + " TEXT , " +
  40. KEY_UPASSWORD + " TEXT , " +
  41. KEY_UEMAILADDRESS + " TEXT );" ;
  42.  
  43. private static final String DATABASE_ACCOUNT_TABLE1 = "CREATE TABLE" + DATABASE_TABLE1 + " (" +
  44. KEY_ROWID1 + " INTEGER PRIMARY KEY, " +
  45. KEY_BANKNAME + " TEXT , " +
  46. KEY_TYPE + " TEXT , " +
  47. KEY_ACCNUM + " TEXT , " +
  48. KEY_BALANCE + " TEXT , " +
  49. KEY_EXPIRYDATE + " TEXT );" ;
  50.  
  51.  
  52. private static class DbHelper extends SQLiteOpenHelper{
  53.  
  54.  
  55.  
  56. public DbHelper(Context context) {
  57. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  58. // TODO Auto-generated constructor stub
  59. }
  60.  
  61. @Override
  62. public void onCreate(SQLiteDatabase db) {
  63.  
  64. db.execSQL(DATABASE_USER_TABLE);
  65. db.execSQL(DATABASE_ACCOUNT_TABLE1);
  66.  
  67.  
  68. }
  69.  
  70.  
  71. @Override
  72. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  73. // TODO Auto-generated method stub
  74. db.execSQL("DROP TABLE IF EXISTS " + DATABASE_USER_TABLE);
  75. db.execSQL("DROP TABLE IF EXISTS" + DATABASE_ACCOUNT_TABLE1);
  76. onCreate(db);
  77.  
  78. }
  79. }
  80.  
  81. public DatabaseAdapter(Context c){
  82. MContext = c;
  83. }
  84.  
  85. public DatabaseAdapter open(){
  86. MHelper = new DbHelper(MContext);
  87. Mdatabase = MHelper.getWritableDatabase();
  88. return this;
  89. }
  90.  
  91. public void close(){
  92.  
  93. MHelper.close();
  94. }
  95. public Cursor fetchListItems() {
  96.  
  97. Cursor cursor = Mdatabase.query(DATABASE_TABLE1, new String[]
  98. { KEY_ROWID1, KEY_BANKNAME, KEY_TYPE, KEY_BALANCE},
  99. null, null, null, null, null);
  100.  
  101. if (cursor != null) {
  102. cursor.moveToFirst();
  103. }
  104. return cursor;
  105. }
  106.  
  107. DatabaseAdapter dbHelper = new DatabaseAdapter(this);
  108. dbHelper.open();
  109.  
  110. // Get a Cursor for the list items
  111. Cursor listCursor = dbHelper.fetchListItems();
  112. startManagingCursor(listCursor);
  113.  
  114. // set the custom list adapter
  115. setListAdapter(new MyListAdapter(this, listCursor));
  116. }
  117.  
  118. private class MyListAdapter extends ResourceCursorAdapter {
  119.  
  120. public MyListAdapter(Context context, Cursor cursor) {
  121. super(context, R.layout.list_item_with_description, cursor);
  122. }
  123.  
  124. @Override
  125. public void bindView(View view, Context context, Cursor cursor) {
  126.  
  127. TextView title = (TextView) view.findViewById(R.id.item_title);
  128. title.setText(cursor.getString(
  129. cursor.getColumnIndex(DatabaseAdapter.KEY_BANKNAME)));
  130.  
  131. TextView details = (TextView) view.findViewById(R.id.item_details);
  132. StringBuffer detailsText = new StringBuffer();
  133.  
  134. int price = cursor.getInt(cursor.getColumnIndex(DatabaseAdapter.KEY_BALANCE));
  135. if (price > 0){
  136. detailsText.append("$"+price+".00");
  137. } else {
  138. detailsText.append("Price Unavailable");
  139. }
  140. String description = cursor.getString(cursor.getColumnIndex(
  141. DatabaseAdapter.KEY_TYPE));
  142. if (description != null && description.length() > 0){
  143. detailsText.append(", "+description);
  144. }
  145. details.setText(detailsText.toString());
  146.  
  147. }
  148.  
  149. }
  150.  
  151. }
Add Comment
Please, Sign In to add comment