我在我的android项目中创建了一个SQLite数据库,其中包含表" Book"包含列id,isbn,title,resume。 我想检索所有条目的isbn,title,resume并在片段的布局中显示它们。
在片段书籍中,代码行BookDAO livreBdd = new BookDAO(this);给出错误。我需要提出一个背景,但是这个"这个"没有工作," getActivity()。getApplicationContext()"都不是。
第二件事,我将在数据库中添加数据,我将使用getAllBooks()方法检索数据库中的所有书籍,每本书都包含信息" isbn"," title&#34 ;和"恢复"我想在片段的布局中显示,我将有一个List,我怎么能使用SimpleCursorAdapter来编码?
DataBaseHandler的代码:
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String BOOK_KEY = "id";
public static final String BOOK_ISBN = "isbn";
public static final String BOOK_TITLE = "title";
public static final String BOOK_RESUME = "resume";
public static final String BOOK_TABLE_NAME = "Book";
public static final String BOOK_TABLE_CREATE = "" +
"CREATE TABLE " + BOOK_TABLE_NAME + "(" +
BOOK_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
BOOK_ISBN + " INTEGER, " +
BOOK_TITLE + " TEXT NOT NULL, "+
BOOK_RESUME + " TEXT );";
public static final String BOOK_TABLE_DROP = "DROP TABLE IF EXISTS" + BOOK_TABLE_NAME + ";";
public DatabaseHandler (Context context, String name, CursorFactory factory, int version){
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(BOOK_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(BOOK_TABLE_DROP);
onCreate(db);
}
}
DAOBase代码:
public abstract class DAOBase {
protected final static int VERSION = 1;
protected final static String NOM = "data.db";
protected SQLiteDatabase mdb = null;
protected DatabaseHandler mHandler = null;
public DAOBase(){
super();
}
public DAOBase (Context context){
super();
this.mHandler = new DatabaseHandler(context, NOM, null, VERSION);
}
public SQLiteDatabase open(){
mdb = mHandler.getWritableDatabase();
return mdb;
}
public void close(){
mdb.close();
}
public SQLiteDatabase getMdb(){
return mdb;
}
}
BookDAO的代码:
public class BookDAO extends DAOBase{
public static final String TABLE_NAME = "Book";
public static final String KEY = "id";
public static final String ISBN = "isbn";
public static final String TITLE = "title";
public static final String RESUME = "resume";
public static final String TABLE_CREATE =
"CREATE TABLE " +
TABLE_NAME + " (" + KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
ISBN + " INTEGER, " +
TITLE + " TEXT NOT NULL," +
RESUME + "TEXT );";
public static String TABLE_DROP = " DROP TABLE IF RXISTS" +
TABLE_NAME + ";";
/*Add a book*/
public long insertBook(Book livre){
ContentValues values = new ContentValues();
values.put(ISBN, livre.getIsbn());
values.put(TITLE, livre.getTitle());
values.put(RESUME, livre.getResume());
return mdb.insert(TABLE_NAME, null, values);
}
/*Update*/
public int updateBook(Book livre) {
ContentValues values = new ContentValues();
values.put(ISBN, livre.getIsbn());
values.put(TITLE, livre.getTitle());
values.put(RESUME, livre.getResume());
return mdb.update(TABLE_NAME, values, KEY + " = ?", new String[]
{String.valueOf(livre.getId())});
}
/*Delete a book*/
public int removeBookWithID(int id){
return mdb.delete(TABLE_NAME, KEY + " = ?" , new String[]
{String.valueOf(id)});
}
/*Get all books*/
public List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<Book>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = mdb.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Book livre = new Book();
livre.setId(Integer.parseInt(cursor.getString(0)));
livre.setIsbn(Integer.parseInt(cursor.getString(1)));
livre.setTitle(cursor.getString(2));
livre.setResume(cursor.getString(3));
bookList.add(livre);
} while (cursor.moveToNext());
}
cursor.close();
return bookList;
}
}
书的代码:
public class Book {
private int id;
private int isbn;
private String title;
private String resume;
public Book(){
}
public Book ( int isbn, String title, String resume){
super();
this.isbn = isbn;
this.title = title;
this.resume = resume;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public int getIsbn(){
return isbn;
}
public void setIsbn(int isbn){
this.isbn = isbn;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getResume(){
return resume;
}
public void setResume(String resume){
this.resume = resume;
}
}
片段代码和应该显示数据时的布局:
public class Books extends Fragment {
public Books() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_books, container, false);
return v;
BookDAO livreBdd = new BookDAO(this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.nabtech.android.nabilaffoauthor.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:id="@+id/booktitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
<TextView
android:id="@+id/bookisbn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
<TextView
android:id="@+id/bookresume"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
</LinearLayout>
答案 0 :(得分:0)
在片段书籍中,代码行BookDAO livreBdd = new BookDAO(this); 给出错误。我需要提出一个背景,但是这个&#34;这个&#34;没有工作
使用BookDAO livreBdd = new BookDAO(getActivity);
但是,它不能在方法的return
之后,代码无法运行,可能是错误的真正原因。
但是,您可能希望在Fragment的onCreate
方法中执行此操作。 (见下文)
第二件事,我将在数据库中添加数据,我将使用getAllBooks()方法检索数据库中的所有书籍,每本书都包含信息&#34; isbn&#34;,&#34; title&#34 ;和&#34;恢复&#34;我想在片段的布局中显示,我将有一个List,我怎么能使用SimpleCursorAdapter来编码?
1)在BookDAO
类中创建一个方法,将提取的数据作为Cursor返回,例如
/*Get all books as Cursor*/
public Cursor getAllBooksAsCursor() {
String selectQuery = "SELECT * FROM " + TABLE_NAME;
return mdb.rawQuery(selectQuery, null);
}
2)
public class Books extends Fragment {
BooDAO livreBdd; //<<<< Added
ListView youlistview; //<<<< Added
SimpleCursorAdapter mSCA; //<<<< Added
Cursor booklist; //<<<< Added
public Books() {
// Required empty public constructor
}
//<<<< onCreate Method Added
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
livreBdd = new BookDAO(getActivity); //<<<< first part of Question
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_books, container, false);
//<<< All lines below added until return
yourlistview = (ListView) view.findViewById(R.id.yourlistview);
booklist = livereBdd.getAllBooksAsCursor();
mSCA = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, //<<<< see note1
booklist, //<<<< source cursor
new String[] {BookDAO,TITLE}, //<<<< column list see note1
new int[]{android.R.id.text1}, //<<<< view list see note1
0 //<<<< flags use 0
);
// Tie the adapter to the Listview
yourlistview.setAdapter(mSCA);
return v;
//BookDAO livreBdd = new BookDAO(this); Can't have this after return.
}
}
注释
1)上面的示例使用将显示1项的内置布局。但是,您可能希望指定自己的多列布局。如果是,则使用列列表指定列名,如在游标中,作为字符串数组的附加元素。对于列列表中的每个元素,视图列表整数数组中需要有一个相应的元素,它是将接收数据的视图的id。
上述内容未经测试,因此可能包含错误,主要用作原则代码。