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);
}

}

データベースから行を取得しようとしましたが、エラーが発生しました。助けてください。私はAndroidを初めて使用します。ここに画像の説明を入力してください

4

2 に答える 2

1

ArrayList(index = 3)の3番目の要素を呼び出していると表示されますが、サイズが0であるため、範囲外です。この種の例外を回避するためArrayListに、にデータを追加したことを確認することをお勧めします。または、機能を実行する前に、が空でない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 に答える