0

对不起,我是新人。我的数据库中有 2 个表:用户和帐户,我已成功将数据添加到数据库。我想从帐户表中检索数据并将其显示到列表视图中。但它不起作用。但是在编辑代码并尝试为用户检索数据后,它在列表视图中正确显示,但它不适用于帐户表。我不知道错误在哪里。你能帮忙吗?感谢 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;
    }
  }

事务列表视图.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());

        }

    }

}

错误:

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

2 回答 2

0

查看您的代码的猜测:

getColumnIndex(DatabaseAdapter.KEY_ACCNUM)

那不是返回您认为的列索引。在您的代码中抛出一条日志语句,然后查看该方法的值是什么。

于 2012-10-07T14:33:12.807 回答
0

首先,提供完整的堆栈,而不仅仅是单行错误。

其次,如果您从字段的名称常量中删除空格,这是一个好的开始。轻轻地在字符串中占用空格是一个坏主意。

于 2012-10-07T15:14:34.767 回答