2

I'm having a bit of trouble figuring out how to bind data from sqlite to a listview.
Here my entire code and I'd be very thankful for some help or tips.
The trouble here is that i'm getting the following exception:
Illegalargumentexception: the column "_id" does not exist, when calling GetCursor in SimpleCursorAdapter.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">    
   <ListView
      android:id="@android:id/android:list"
      android:layout_width="fill_parent"
     android:layout_height="wrap_content" />

</LinearLayout>

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="24dp"
    android:padding="6dp" />
<TextView
    android:id="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18dp"
    android:padding="1dp" />
<TextView
    android:id="@+id/date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18dp"
    android:padding="4dp"
    android:gravity="right" />
</LinearLayout>

Class implementing SQLite

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";      

private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private Cursor cursor;

public DataHelper(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
}

public long insert(String name, String time, String date) {
    insertStmt = db.compileStatement("insert into table1 values(?,?,?)");       
    this.insertStmt.bindString(1, name);
    this.insertStmt.bindString(2, time);
    this.insertStmt.bindString(3, date);
    return this.insertStmt.executeInsert(); 
}

public void deleteAll() {
    this.db.delete(TABLE_NAME, null, null);
}

public Cursor GetCursor() {
    cursor = this.db.query(TABLE_NAME, new String[] { "_id", "name", "time", "date" }, null, null, null, null, "date desc");

    return cursor;      
}

private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "( _id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, time TEXT, date TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("Example",
                "Upgrading database, this will drop tables and recreate.");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }
    }
}

onCreate class

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class SqlTest extends ListActivity {

private DataHelper dh;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Date now = new Date();
    String time = now.getHours() + ":" + now.getMinutes();
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    String date = df.format(now);

    this.dh = new DataHelper(this);
    this.dh.deleteAll();
    this.dh.insert("Porky Pig", time, date);
    this.dh.insert("Foghorn Leghorn", time, date);
    this.dh.insert("Yosemite Sam", time, date);
    this.dh.insert("SD", time, date);

    String[] columns = new String[] { "_id", "name", "time", "date" };
    int[] to = new int[] {R.id.name, R.id.time, R.id.date};

    SimpleCursorAdapter mAdap = new SimpleCursorAdapter(this, R.layout.main, this.dh.GetCursor(), columns, to);

    this.setListAdapter(mAdap);
}
}
2
  • What's the specific concern/question you have? What isn't working? What is? Does it crash? Are there log messages? Just posting your code doesn't really say any of that. Commented Dec 18, 2011 at 18:00
  • Sorry guys, the exception I'm getting is an illegalargument exception saying the column "_id" does not exist, for the simplecursoradapter. Commented Dec 18, 2011 at 18:29

1 Answer 1

1

Try this:

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class SqlTest extends ListActivity {

private DataHelper dh;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Date now = new Date();
    String time = now.getHours() + ":" + now.getMinutes();
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    String date = df.format(now);

    this.dh = new DataHelper(this);
    this.dh.deleteAll();
    this.dh.insert("Porky Pig", time, date);
    this.dh.insert("Foghorn Leghorn", time, date);
    this.dh.insert("Yosemite Sam", time, date);
    this.dh.insert("SD", time, date);

    // Don't put _id here
    String[] columns = new String[] {"name", "time", "date" };
    int[] to = new int[] {R.id.name, R.id.time, R.id.date};

    SimpleCursorAdapter mAdap = new SimpleCursorAdapter(this, R.layout.main, this.dh.GetCursor(), columns, to);

    this.setListAdapter(mAdap);
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Specify whats changed next time plz.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.