Populating a listView with SQLite database using simple cursor adapter
<p>I have a SQLite database with 3 columns and a ListView. I want to populate the listView with SQLite. I think my coding is true but my app crashes when I want to see the listView.
below is my sample code:</p>

<p><strong>MainActivity.java</strong></p>

<pre><code>public class MainActivity extends ActionBarActivity {
Registration register;
EditText name, grade;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name = (EditText) findViewById(R.id.editName);
    grade = (EditText) findViewById(R.id.editGrade);
    register = new Registration(this);
        }

public void addButton(View view) {
    String eName = name.getText().toString();
    String eGrade = grade.getText().toString();
    register.insert(eName, eGrade);
    name.setText("");
    grade.setText("");
    Toast.makeText(this, "Item added", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    if (id == R.id.sqlView) {
        Intent intent = new Intent(this, ViewDatabase.class);
        startActivity(intent);
    }

    return super.onOptionsItemSelected(item);
}}
</code></pre>

<p><strong>sqlOpenHelper.java</strong></p>

<pre><code>public class sqlOpenHelper extends SQLiteOpenHelper{

public static final String DATABASE_NAME = "FirstBase.db";
public static final String TABLE_NAME = "First_table";
public static final int VERSION = 1;
public static final String C_KEY_ID = "ID";
public static final String C_name = "Name";
public static final String C_grade = "Grade";
public static final String SCRIPT = "CREATE TABLE " + TABLE_NAME + " ( " +
        C_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        C_name + " TEXT NOT NULL, " +
        C_grade + " TEXT NOT NULL " + ");";



public sqlOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(SCRIPT);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String DELETE_SCRIPT = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(DELETE_SCRIPT);
    onCreate(db);
}}
</code></pre>

<p><strong>Registration.java</strong></p>

<pre><code>  public class Registration {
   SQLiteDatabase db;
   sqlOpenHelper oh;
   Context context;

public Registration(Context context) {
    this.context = context;
}


public Cursor print() {
    oh = new sqlOpenHelper(context, oh.DATABASE_NAME, null, oh.VERSION);
    String[] columns = {oh.C_KEY_ID, oh.C_name, oh.C_grade};
    db = oh.getWritableDatabase();
    Cursor cursor = db.query(sqlOpenHelper.TABLE_NAME, columns, null, null, null, null, null);
    return cursor;

}
public void insert(String name, String grade) {
    ContentValues values = new ContentValues();
    values.put(oh.C_name, name);
    values.put(oh.C_grade, grade);
    oh = new sqlOpenHelper(context, oh.DATABASE_NAME, null, oh.VERSION);
    db = oh.getWritableDatabase();
    db.insert(oh.TABLE_NAME, null, values);
    db.close();
}}
</code></pre>

<p><strong>ViewDatabase.java</strong></p>

<pre><code>public class ViewDatabase extends ActionBarActivity {

private static final String[] from = new String[] {
        sqlOpenHelper.C_KEY_ID,
        sqlOpenHelper.C_name,
        sqlOpenHelper.C_grade};
private static final int[] to = new int[] {
        R.id.rId,
        R.id.rName,
        R.id.rGrade};

Registration register;
sqlOpenHelper oh;
SQLiteDatabase db;
Cursor cursor;
SimpleCursorAdapter cursorAdapter;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_database);
    list = (ListView) findViewById(R.id.listView);
    oh = new sqlOpenHelper(this, oh.DATABASE_NAME, null, oh.VERSION);
    db = oh.getReadableDatabase();
    register = new Registration(this);
    cursor = register.print();
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.rows, cursor, from, to);
    list.setAdapter(cursorAdapter);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_database, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}
</code></pre>

<p><strong>activity_main.xml</strong></p>

<pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"&gt;

&lt;LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:orientation="horizontal"
    android:id="@+id/name"&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="Name"
        android:textSize="20sp" /&gt;

    &lt;EditText
        android:id="@+id/editName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Name" /&gt;

&lt;/LinearLayout&gt;

&lt;LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:orientation="horizontal"
    android:layout_below="@id/name"&gt;

    &lt;TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="Grade"
        android:textSize="20sp" /&gt;

    &lt;EditText
        android:id="@+id/editGrade"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number"
        android:hint="Grade"/&gt;
&lt;/LinearLayout&gt;

&lt;Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Add"
    android:onClick="addButton"/&gt;
&lt;/RelativeLayout&gt;
</code></pre>

<p><a href="http://i.stack.imgur.com/Gar1f.png" rel="nofollow">enter image description here</a></p>

<p><strong>rows.xml</strong></p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"&gt;

&lt;TextView
    android:id="@+id/rId"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="30sp"/&gt;

&lt;TextView
    android:id="@+id/rName"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="30sp"/&gt;

&lt;TextView
    android:id="@+id/rGrade"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="30sp"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p><strong>adtivity_view_database.xml</strong></p>

<pre><code>   &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.brothers.sql.ViewDatabase"&gt;

&lt;ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p><strong>Hear is the logcat</strong></p>

<pre><code> java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brothers.sql/com.example.brothers.sql.ViewDatabase}: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
</code></pre>