无法将sqlliteData-base中的数据绑定到simplecursoradaptor

时间:2015-02-28 01:16:35

标签: java android

我不知道如何连接数据我的数组String [] station = {“København”,“Grenaa”,“Hanstholm”};在我的MyListActivity中使用simpleCursorAdaptor 我已经创建了一个SQLitedatabase,一个Helperclass和一个活动类 - 但我收到错误“java.lang.IllegalArgumentException:column'København'不存在”。我有额外的代码 - 但我认为这个代码应该足够了。 任何帮助都会非常感激。

public class MyListActivity extends ListActivity {
String [] station = {"København", "Grenaa", "Hanstholm"};
Cursor stations;
SQLiteDatabase db;
SimpleCursorAdapter cursorAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DBAdapter  dbaAdapter = new DBAdapter(this);
    dbaAdapter.open();
    Cursor stations = dbaAdapter.getStations();

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,stations,station,new int [] {
            android.R.id.text1
    });
    setListAdapter(cursorAdapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Cursor cursor = (Cursor) l.getItemAtPosition(position);
    String value = station[(int)id];
    Intent intent = new Intent();
    intent.putExtra(TravelActivity.SELECTED_STATION_NAME, cursor.getString(cursor.getColumnIndexOrThrow("station")));
    this.setResult(RESULT_OK,intent);

    cursor.close();
    finish();


}

@Override
protected void onDestroy() {
    db.close();

}

}

public class MyHelper extends SQLiteOpenHelper {

  public static final String DB_NAME = "database";
    String DESTINATION = "DESTINATION";
    int version = 1;



    public MyHelper(Context context) {

        super(context, "travel.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String TRAVELS = ( "create table travels (_id integer primary key autoincrement, start text, slut text)");
        String STATIONS = ( "create table stations (_id integer primary key autoincrement, start text)" );
        db.execSQL(TRAVELS);
        db.execSQL(STATIONS);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS ");
        onCreate(db);

    }
}



public class DBAdapter {
    MyHelper myHelper;
    SQLiteDatabase db;

    String TABLE_STATIONS = "stations";
    String TABLE_TRAVELS = "travels";
    String START = "start";
    String SLUT = "slut";
    String ID_COL = "_id";
    Context context;

    public static final int NUMBER_TRAVELS = 1;


    public DBAdapter(Context context) {
       this.context = context;
        myHelper = new MyHelper(context);

    }

    public void open() {
        db = myHelper.getWritableDatabase();

    }

    public void close() {
        myHelper.close();
    }

    public Cursor getTravels() {
        Cursor cursor = db.query(TABLE_TRAVELS,new String[]{ID_COL,START,SLUT},null,null,null,null,START);
      return cursor;
    }


    public void saveTravels(String start, String slut) {
        ContentValues values = new ContentValues();
        values.put(START,start);
        values.put(SLUT,slut);
        db.insert(TABLE_TRAVELS,null,values);



    }

    public Cursor getStations() {
        Cursor cursor = db.query(TABLE_STATIONS,new String[]{ID_COL,START},null,null,null,null,START);
        return cursor;

    }

    public void saveStations(String start) {
        ContentValues values = new ContentValues();
        values.put(START,start);
        db.insert(TABLE_TRAVELS,null,values);
    }

}

1 个答案:

答案 0 :(得分:0)

错误由SimpleCursorAdapter构造函数生成:

  

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,   android.R.layout.simple_list_item_1,stations,station,new int [] {               android.R.id.text1       });

4td参数是列名,因此在您的情况下具有START和/或SLUT值的String数组。