使用SQLite填充列表视图

时间:2014-12-09 16:36:52

标签: android android-listview android-sqlite

嘿伙计我试图使用SQlite填充我的列表视图但是每次运行应用程序时都会崩溃。以下是文件:

private void populateListView() {

    Cursor cursor = mydb.getAllEvents();

    String[] fromFieldNames = new String[]
            {DBHelper.EVENTS_COLUMN_NAME,DBHelper.EVENTS_COLUMN_CATEGORY};

    int[] toViewIDs = new int[]
            {R.id.event_name, R.id.cat_name};

    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new   SimpleCursorAdapter(this,R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
    ListView myList = (ListView) findViewById(R.id.listView1);

    myList.setAdapter(myCursorAdapter);
}

数据库:

package team08.httpapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Events.db";
public static final String EVENTS_TABLE_NAME = "events";
public static final String EVENTS_COLUMN_ID = "_id";
public static final String EVENTS_COLUMN_NAME = "name";
public static final String EVENTS_COLUMN_DATE = "date";
public static final String EVENTS_COLUMN_CATEGORY = "category";
public static final String EVENTS_COLUMN_DESCRIPTION = "description";
public static final String EVENTS_COLUMN_TIME_START = "time_start";
public static final String EVENTS_COLUMN_TIME_END = "time_end";
public static final String EVENTS_COLUMN_ADDRESS = "address";
public static final String EVENTS_COLUMN_POSTCODE = "postcode";

private HashMap hp;

public DBHelper(Context context)
{
    super(context, DATABASE_NAME , null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table events " +
                    "( _id INTEGER PRIMARY KEY AUTOINCREMENT, name text, date text, category text, description text, time_start text," +
                    " time_end text, address text, postcode text)"
    );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS events");
    onCreate(db);
}

public void clearDatabase(){
   SQLiteDatabase db = this.getWritableDatabase();
   db.execSQL("DROP TABLE IF EXISTS events");
   onCreate(db);

}

public boolean insertEvent  (String name, String date, String category, String description,String time_start,String time_end, String address,String postcode)
{

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put("name", name);
    contentValues.put("date", date);
    contentValues.put("category", category);
    contentValues.put("description", description);
    contentValues.put("time_start", time_start);
    contentValues.put("time_end", time_end);
    contentValues.put("address", address);
    contentValues.put("postcode", postcode);

    db.insert("events", null, contentValues);
    return true;
}
public ArrayList getEvent(int id)
{
    ArrayList array_list = new ArrayList();
    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from events WHERE _id="+id+"", null );
    res.moveToFirst();

    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_NAME)));
    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_DATE)));
    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_DESCRIPTION)));
    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_TIME_START)));
    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_TIME_END)));
    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_ADDRESS)));
    array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_POSTCODE)));

    return array_list;
}
public int numberOfRows(){
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, EVENTS_TABLE_NAME);
    return numRows;
}

public Cursor getAllEvents()
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from events", null );
    return res;
}
}

我的logcat说:

java.lang.RuntimeException: Unable to start activity ComponentInfo{team08.httpapp/team08.httpapp.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist

我读到SimpleCursorAdapter在数据库中需要一个_id字段,所以我添加了它但是它仍然无法工作。

1 个答案:

答案 0 :(得分:0)

您之后是添加了_id列还是仅添加了第一次?

If Added later then
You need to Uninstall app once so old database will get cleared & next time _id column will generate.