无法将sqllite数据显示到表格布局中

时间:2015-06-16 06:13:00

标签: android android-sqlite android-database

我是Android. I'm trying to display the data in a tabular form, loading data from the数据库的新手。但它显示以下错误:

 java.lang.IllegalStateException: Couldn't read row 0, col -1 from  
    CursorWindow.  Make sure the Cursor is initialized correctly before  
    accessing data from it.
            at android.database.CursorWindow.nativeGetString(Native Method)
            at android.database.CursorWindow.getString(CursorWindow.java:438)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
            at com.example.rishii.introduction.Main_activity.addData(Main_activity.java:107)
            at com.example.rishii.introduction.Main_activity$1.onClick(Main_activity.java:67)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

以下发布的代码适用于Database helper class

public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME ="db1";
public static  final int DATABASE_VERSION=1;
public static final String TABLE_NAME ="Student";
public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS  
Student(Username varchar(20),Password varchar(20))";
public static final String DELETE_TABLE="DROP TABLE IF EXISTS " + 
TABLE_NAME;

public DataBaseHelper(Context context)
{
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
  db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL(DELETE_TABLE);
    onCreate(db);
    }
  public void insertData(String Username,String Password)
   {
    SQLiteDatabase db = this.getWritableDatabase();
    db.beginTransaction();
    ContentValues values;

    try
    {
        values = new ContentValues();
        values.put("Username",Username);
        values.put("Password",Password);
        long i =db.insert(TABLE_NAME,null,values);
        Log.i("Insert",i+"");
        db.setTransactionSuccessful();
    }
    catch (SQLiteException e)
    {
        e.printStackTrace();
    }
    finally {
        db.endTransaction();
        db.close();
    }
    }
    }

以下是Mainactivity.java

的代码
    EditText user = (EditText) findViewById(R.id.username);
    EditText pwd =  (EditText) findViewById(R.id.password);
    String username = user.getText().toString();
    String password = pwd.getText().toString();
    DataBaseHelper hl = new DataBaseHelper(context);
    hl.insertData(username, password);
    TableLayout tableLayout = (TableLayout)findViewById(R.id.tablelayout);
    TableRow tableRow = new TableRow(context);
    tableRow.setBackgroundColor(Color.parseColor("#c0c0c0"));
    tableRow.setLayoutParams(new  
    TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,   
    TableLayout.LayoutParams.WRAP_CONTENT));
    String[]Header ={"Username","Password"};
    for(String c:Header)
    {
        TextView f =new TextView(this);
        f.setGravity(Gravity.CENTER);
        f.setTextSize(18);
        f.setPadding(5, 5, 5, 5);
        f.setText(c);
        tableRow.addView(f);
    }
    tableLayout.addView(tableRow);
     db = hl.getWritableDatabase();
    db.beginTransaction();
    try
    {

        String select = "SELECT*from Student";
        Cursor c = db.rawQuery(select,null);
        if(c.getCount()>0)
        {
            while (c.moveToNext())
            {
                String userr = c.getString(c.getColumnIndex("Username"));
                String passwrd = c.getString(c.getColumnIndex("Password"));
                TableRow row  = new TableRow(context);
                row.setLayoutParams(new 


TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
                String [] colText = {userr,  passwrd};
                for(String text:colText)
                {
                    TextView v = new TextView(this);
                    v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
                    v.setGravity(Gravity.CENTER);
                    v.setTextSize(16);
                    v.setPadding(5,5,5,5);
                    v.setText(text);
                    row.addView(v);
                }
               tableLayout.addView(row);


            }
        }
        db.setTransactionSuccessful();
        c.close();
    }
    catch (SQLiteException e)
    {
        e.printStackTrace();
    }
    finally {

        db.endTransaction();
        db.close();
      }

2 个答案:

答案 0 :(得分:1)

try {
    String select = "SELECT*from Student";
    Cursor c = db.rawQuery(select,null);

    if (c.getCount() > 0) {
        c.moveToFirst();

        while (c.moveToNext()){
          ..............
        }
    }
    db.setTransactionSuccessful();
    c.close();
}

答案 1 :(得分:1)

java.lang.IllegalStateException: Couldn't read row 0, col -1

在上面一行中,明确指出列索引是-1

String userr = c.getString(c.getColumnIndex("Username"));

在您的情况下,c.getgetColumnIndex(-)返回-1。

卸载您的应用后再次检查。

希望这会对你有所帮助。