0

这是我的数据库管理器类

public class MyDbManager {

private String DB_NAME="MyDb";
private String TABLE="TableReg";
private int DB_VERSION=1;
//private String ID="id";
private String Fname="Fname";
private String Lname="Lname";
private String Email="Email";
private String Password="Password";
private String Language="Languages";
private String Gender ="gender"; 
private String Id="id";

private SQLiteDatabase sdb;
Context context;
public MyDbManager(Context context) {
    // TODO Auto-generated constructor stub
    this.context=context;
    CustomSQLiteOpenHelper helper= new CustomSQLiteOpenHelper(context);
    this.sdb=helper.getWritableDatabase();      
}

public void insertValues(String fn,String ln, String em, String pwd)
{
    ContentValues values=new ContentValues();
    values.put(Fname, fn);
    values.put(Lname, ln);
    values.put(Email, em);
    values.put(Password, pwd);

// values.put(Language, lang); // values.put(Gender, gen);

    sdb.insert(TABLE, null, values);
}


public ArrayList<Object> getRow(String email)
{
    // create an array list to store data from the database row.
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead.  That way you can ensure
    // data types are correct.
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {

        cursor = sdb.query
        (
                TABLE,
                new String[]{Id,Fname,Lname,Email,Password},
                Email + "=" + email,
                null, null, null, null, null
        );

        // move the pointer to position zero in the cursor.
        cursor.moveToFirst();

        // if there is data available after the cursor's pointer, add
        // it to the ArrayList that will be returned by the method.
        if (!cursor.isAfterLast())
        {
            do
            {
                rowArray.add(cursor.getLong(0));
                rowArray.add(cursor.getString(1));
                rowArray.add(cursor.getString(2));
                rowArray.add(cursor.getString(3));
                rowArray.add(cursor.getString(4));

            }
            while (cursor.moveToNext());
        }

        // let java know that you are through with the cursor.
        cursor.close();
    }
    catch (SQLException e) 
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList containing the given row from the database.
    return rowArray;
}
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
    public CustomSQLiteOpenHelper(Context context)
    {
        super(context,DB_NAME,null,DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

        String newTableQueryString = " create table " + 
                                      TABLE + 
                                      " (" +
                                       Id + " integer primary key autoincrement not null," +
                                      Fname + " text," + 
                                      Lname + " text," + 
                                      Email + " text," + 
                                      Password + " text" +
                                      ");";


        db.execSQL(newTableQueryString);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        //
    }
}

}

我试图检索该行的活动:

public class Login extends Activity {

MyDbManager dbm;
EditText ed_em,ed_ps;
Button btn_sgn_in;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    dbm=new MyDbManager(this);
    setupviews();
    btn_sgn_in.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            retrieveRow();
        }
    });

}

public void retrieveRow()
{
    String Email=ed_em.getText().toString();
    ArrayList<Object> row;
    row=dbm.getRow(Email);
    String r1=(String)row.get(3);
    Toast.makeText(getBaseContext(), r1, Toast.LENGTH_LONG).show();

}
public void setupviews()
{
    ed_em=(EditText)findViewById(R.id.ed_email_ver);
    ed_ps=(EditText)findViewById(R.id.ed_pass_ver);
    btn_sgn_in=(Button)findViewById(R.id.btn_sgn_in);
}

}

我试图从数据库中检索一行,但出现错误。请帮助我。我是安卓新手。在此处输入图像描述

4

2 回答 2

1

它说您正在调用ArrayList(index=3) 的第三个元素,但您的大小ArrayList为 0,因此您超出范围,您可能需要确保已在其中添加数据ArrayList以避免此类异常或者在你做任何你的功能之前检查是否ArrayList不为空。祝你好运。

于 2012-10-02T05:08:45.143 回答
1

If条件下先检查arraylist的大小

if(arraylist.size()>3)
{
     //use arraylist here
}

并将您在 dbhelper 中的方法更改为

public ArrayList<Object> getRow(String email)
{
    // create an array list to store data from the database row.
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead.  That way you can ensure
    // data types are correct.
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {
        Cursor cursor = this.db.query(TABLE, new String[] { "Id,Fname,Lname,Email,Password" },
                "Email =  \"" + email +"\" " , null, null, null,null);

        if (cursor.moveToFirst()) {
             do {
                    rowArray.add(cursor.getLong(0));
                    rowArray.add(cursor.getString(1));
                    rowArray.add(cursor.getString(2));
                    rowArray.add(cursor.getString(3));
                    rowArray.add(cursor.getString(4));              


               } while (cursor.moveToNext());
          }
          if (cursor != null && !cursor.isClosed()) {
             cursor.close();
          }
    }
    catch (SQLException e) 
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList containing the given row from the database.
    return rowArray;
}
于 2012-10-02T05:13:04.597 回答