Search code examples
androidandroid-listviewandroid-cursoradapterandroid-sqlite

Cannot retrieve data from database and display in listview


Sorry im new.. I have 2 tables in my database: User and Account, I have successfully add data to database.I want to retrieve data from the account table and display it to a list view. But it is not working. But after editing the code and try to retrieve data for the user it display correctly in the list view but it does not work for the account table. I don't know where the error is. Can you help please. thanks DatabaseAdapter.java

//User Table
public static final String KEY_ROWID = "_id";
public static final String KEY_UNAME = "name";
public static final String KEY_USURNAME = "surname";
public static final String KEY_UUSERNAME = "username";
public static final String KEY_UPASSWORD = "password";
public static final String KEY_UEMAILADDRESS = "emailadd";

//Account Table
public static final String KEY_ROWID1 = "_id";
public static final String KEY_BANKNAME =" bankname";
public static final String KEY_TYPE = " type";
public static final String KEY_ACCNUM = " accnum";
public static final String KEY_BALANCE = " balance";
public static final String KEY_EXPIRYDATE = " expirydate";

private static final String DATABASE_NAME = "MoneyManagerSys";
public static final String DATABASE_TABLE = " Usertb";
public static final String DATABASE_TABLE1 = " Accounttb";
private static final String DATABASE_TABLE2 = " Transactiontb";
private static final String DATABASE_TABLE3 = " BillRemindertb";
//Database Version
private static final int DATABASE_VERSION = 1;
private static String Usertb;
private static String Accounttb;
    private DbHelper MHelper;
private final Context MContext;
private SQLiteDatabase Mdatabase;

private static final String DATABASE_USER_TABLE = "CREATE TABLE" + DATABASE_TABLE + "  (" +
        KEY_ROWID + " INTEGER PRIMARY KEY, " +
        KEY_UNAME + " TEXT , " +
        KEY_USURNAME + " TEXT , " +
        KEY_UUSERNAME + " TEXT , " +
        KEY_UPASSWORD + " TEXT , " +
        KEY_UEMAILADDRESS + " TEXT );" ;

private static final String DATABASE_ACCOUNT_TABLE1 = "CREATE TABLE" + DATABASE_TABLE1 + " (" +
        KEY_ROWID1 + " INTEGER PRIMARY KEY, " +
        KEY_BANKNAME + " TEXT , " +
        KEY_TYPE + " TEXT , " +
        KEY_ACCNUM + " TEXT , " +
        KEY_BALANCE + " TEXT , " +
        KEY_EXPIRYDATE + " TEXT  );" ;

       public long createEntry1(String bankname, String type, String accnum, String balance, String expirydate) {
    ContentValues cv1 = new ContentValues();
    cv1.put(KEY_BANKNAME, bankname);
    cv1.put(KEY_TYPE, type);
    cv1.put(KEY_ACCNUM, accnum);
    cv1.put(KEY_BALANCE, balance);
    cv1.put(KEY_EXPIRYDATE, expirydate);
    return Mdatabase.insert(DATABASE_TABLE1, null, cv1);
}

public long createEntry(String name, String surname, String username, String password, String emailadd) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_UNAME, name);
    cv.put(KEY_USURNAME, surname);
    cv.put(KEY_UUSERNAME, username);
    cv.put(KEY_UPASSWORD, password);
    cv.put(KEY_UEMAILADDRESS, emailadd);
    return Mdatabase.insert(DATABASE_TABLE, null, cv);
}


public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[] {KEY_ROWID, KEY_UNAME, KEY_USURNAME, KEY_UUSERNAME, KEY_UPASSWORD, KEY_UEMAILADDRESS};

    Cursor c = Mdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    String result="";
    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iUname = c.getColumnIndex(KEY_UNAME);   
    int iUsurnamne = c.getColumnIndex(KEY_USURNAME);    
    int iUuserpassword = c.getColumnIndex(KEY_UPASSWORD);   
    int iEmailAdd = c.getColumnIndex(KEY_UEMAILADDRESS);    

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(iUname) + " " + c.getString(iUsurnamne) + " " + c.getString(iUsurnamne) + " " + c.getString(iUuserpassword) + " " + c.getString(iEmailAdd) + "\n";
    }
    return result;
}

 public boolean Login(String username, String password) throws SQLException  
    {  
        Cursor mCursor = Mdatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});  
        if (mCursor != null) {  
            if(mCursor.getCount() > 0)  
            {  
                return true;  
            }  
        }  
     return false;  
    }  

  public Cursor fetchListItems() {

        Cursor cursor1 = Mdatabase.query(DATABASE_TABLE1, new String[] 
                      { KEY_ROWID1, KEY_BANKNAME, KEY_ACCNUM, KEY_BALANCE}, 
                      null, null, null, null, null);

        if (cursor1 != null) {
            cursor1.moveToFirst();
        }
        return cursor1;
    }
  }

TransactionListView.java

        public class TransactionListView extends ListActivity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.translistview);
        DatabaseAdapter dbHelper = new DatabaseAdapter(this);
        dbHelper.open();

        // Get a Cursor for the list items
        Cursor listCursor = dbHelper.fetchListItems();
        startManagingCursor(listCursor);

        // set the custom list adapter
        setListAdapter(new MyListAdapter(this, listCursor));
    }

    private class MyListAdapter extends ResourceCursorAdapter {

        public MyListAdapter(Context context, Cursor cursor) {
            super(context, R.layout.list_item_with_description, cursor);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor1) {

            TextView title = (TextView) view.findViewById(R.id.item_title);
            title.setText(cursor1.getString(
                        cursor1.getColumnIndex(DatabaseAdapter.KEY_BANKNAME)));

            TextView details = (TextView) view.findViewById(R.id.item_details);
            StringBuffer detailsText = new StringBuffer();

            int price = cursor1.getInt(cursor1.getColumnIndex(DatabaseAdapter.KEY_ACCNUM));
            if (price > 0){
                detailsText.append("Rs"+price+".00");
            } else {
                detailsText.append("Price Unavailable");
            }
            String description = cursor1.getString(cursor1.getColumnIndex(
                                                    DatabaseAdapter.KEY_BALANCE));
            if (description != null && description.length() > 0){
                detailsText.append(", "+description);
            }
            details.setText(detailsText.toString());

        }

    }

}

ERROR:

   10-07 16:23:10.437: E/CursorWindow(13987): Bad request for field slot 0,-1. numRows = 3,        numColumns = 4

Solution

  • first of all, provide the full stack not just the single line of error.

    second, it's a good beginning if you removed the spaces out of the fields' names constants. it's just a bad idea to take spaces in strings that lightly.