Unable to display sqllite data into Table Layout

119 views Asked by At

I am new to Android. I'm trying to display the data in a tabular form, loading data from thedatabase`. But it is showing the following error:

 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)

Below posted code is for 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();
    }
    }
    }

Here is the code for 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

There are 2 answers

0
Nagaraju V On BEST ANSWER
java.lang.IllegalStateException: Couldn't read row 0, col -1

In the above line it is clearly stating that column index is -1

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

In your case c.getgetColumnIndex(-) is returning -1.

Once uninstall your app and check again.

Hope this will helps you.

0
Bhavin Kevadiya On
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();
}