База общается с ListView через SimpleCursorAdapter. Вот код
public class MainActivity extends AppCompatActivity {
DB db;
SimpleCursorAdapter sca;
Cursor cursor;
ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DB(this);
db.open();
cursor = db.getAllData();
startManagingCursor(cursor);
String[] from = new String[] { DB.COLUMN_PREV_IMG, DB.COLUMN_HEADER, DB.COLUMN_SHORT_TEXT, DB.COLUMN_DATE };
int[] to = new int[] { R.id.prev_img, R.id.tvHeader, R.id.tvShortText, R.id.tvDate };
sca = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(sca);
//listView.setOnItemClickListener(itemClickListener);
//blah
}
protected void onDestroy() {
super.onDestroy();
// закрываем подключение при выходе
db.close();
}
}
код DB.java
public class DB {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 9;
private static final String DB_TABLE = "mytable";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_IMG = "img_id";
public static final String COLUMN_PREV_IMG = "prev_image_id";
public static final String COLUMN_LINK = "link";
public static final String COLUMN_HEADER = "header";
public static final String COLUMN_FULL_TEXT = "full_text";
public static final String COLUMN_SHORT_TEXT = "short_text";
public static final String COLUMN_DATE = "date";
private static final String DB_CREATE =
"create table " + DB_TABLE + "("
+ COLUMN_ID + " integer primary key," + COLUMN_IMG + " integer,"
+ COLUMN_PREV_IMG + " integer," + COLUMN_LINK + " text"
+ COLUMN_HEADER + " text," + COLUMN_FULL_TEXT + " text"
+ COLUMN_SHORT_TEXT + " text," + COLUMN_DATE + " text" + ");";
private final Context mCtx;
private DBHelper DBHelper;
private SQLiteDatabase DB;
public DB(Context ctx) {
mCtx = ctx;
}
// открыть подключение
public void open() {
DBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
DB = DBHelper.getWritableDatabase();
}
// закрыть подключение
public void close() {
if (DBHelper!=null) DBHelper.close();
}
// получить все данные из таблицы DB_TABLE
public Cursor getAllData() {
return DB.query(DB_TABLE, null, null, null, null, null, null);
}
// добавить запись в DB_TABLE
public void addRec(int id, int img, int prevImg, String link, String header, String fullText,
String shortText, String date) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_ID, id);
cv.put(COLUMN_IMG, img);
cv.put(COLUMN_PREV_IMG, prevImg);
cv.put(COLUMN_LINK, link);
cv.put(COLUMN_HEADER, header);
cv.put(COLUMN_FULL_TEXT, fullText);
cv.put(COLUMN_SHORT_TEXT, shortText);
cv.put(COLUMN_DATE, date);
DB.insert(DB_TABLE, null, cv);
}
// удалить запись из DB_TABLE
public void delRec(long id) {
DB.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
}
// класс по созданию и управлению БД
private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, version);
}
// создаем и заполняем БД
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
Log.d("myLogs", "Обновляемся");
ContentValues cv = new ContentValues();
// заполним таблицу
cv.put("_id", 10);
cv.put("img_id", R.drawable.first);
cv.put("prev_img_id", R.drawable.first);
cv.put("link", "blablaLink");
cv.put("header", "title1");
cv.put("full_text", "fulltext1");
cv.put("short_text", "shorttext1");
cv.put("date", "1456754040029");
db.insert("mytable", null, cv);
Log.d("myLogs", "Обновляемся");
cv.put("_id", 11);
cv.put("img_id", R.drawable.second);
cv.put("prev_img_id", R.drawable.second);
cv.put("link", "blablaLink2");
cv.put("header", "title2");
cv.put("full_text", "fulltext2");
cv.put("short_text", "shorttext2");
cv.put("date", "1456754030029");
db.insert("mytable", null, cv);
Log.d("myLogs", "Обновляемся 3");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion != newVersion) {
Log.d("myLogs", "Обновляемся с версии " + oldVersion + " на версию " + newVersion);
// Удаляем старую таблицу и создаём новую
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
// Создаём новую таблицу
onCreate(db);
}
}
}
}
Что мне говорит студия
1) table mytable has no column named header
02-29 11:54:11.004 3380-3380/ru.arcsinus.SalesBlast E/SQLiteDatabase: Error inserting img_id=2130837576 link=blablaLink2 date=1456754030029 full_text=fulltext2 header=title2 short_text=shorttext2 _id=11 prev_img_id=2130837576
02-29 11:54:11.004 3380-3380/ru.arcsinus.SalesBlast E/SQLiteDatabase: android.database.sqlite.SQLiteException: table mytable has no column named header (code 1): , while compiling: INSERT INTO mytable(img_id,link,date,full_text,header,short_text,_id,prev_img_id) VALUES (?,?,?,?,?,?,?,?)