samedi 11 avril 2015

Are my textviews saving in the Sqlite table?

I cannot work out where I am going wrong with this. I have tried every variation of setting my button to work. My tables have been created. But when i click 'Save Book' I can't be sure that the book has been saved. In my Logcat I get the following everytime i click the button. -



04-11 13:33:12.210 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 44149 to 23219
04-11 13:33:12.234 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 55759 to 23219
04-11 13:33:12.258 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 47369 to 23219
04-11 13:33:12.282 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 48979 to 23219
04-11 13:33:12.306 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 30589 to 23219
04-11 13:33:12.354 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 24398 to 23219
04-11 13:33:14.330 124-289/? W/genymotion_audio﹕ out_write() limiting sleep time 29476 to 23219


What I want is the set textviews, to be saved in the sqlite database - saveBooks()


Button xml


Main activity - saveBooks method for button



public class SearchActivity extends Activity implements SearchView.OnQueryTextListener,
SearchView.OnCloseListener {

private SearchView searchView;
private ListView myList;
private BooksDBAdapter mDbHelper;
@SuppressWarnings("rawtypes")

private TextView authorText;
private TextView titleText;
private TextView priceText;
private TextView publishDateText;
private TextView descriptionText;
private TextView moduleText;
private TextView buyText;

private Button saveB;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);

myList = (ListView) findViewById(R.id.listView);

//set searchview
searchView = (SearchView) findViewById(R.id.searchView);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);

mDbHelper = new BooksDBAdapter(this);
mDbHelper.open();

//btnClick();

mDbHelper.deleteAllBooks();

XmlResourceParser xpp = getResources().getXml(R.xml.books);

try {
// mDbHelper = new BooksDBAdapter(this);
// mDbHelper.open();

// get initial eventType

while (xpp.next() != XmlPullParser.END_TAG) {
if (xpp.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = xpp.getName();

if (name.equals("book")) {
String author = null, title = null, price = null, publish_date = null, description = null, module = null, buy = null;
while (xpp.next() != XmlPullParser.END_TAG) {
if (xpp.getEventType() != XmlPullParser.START_TAG) {
continue;
}
name = xpp.getName();
switch (name) {
case "author":
author = readText(xpp);
break;
case "title":
title = readText(xpp);
break;
case "price":
price = readText(xpp);
break;
case "publish_date":
publish_date = readText(xpp);
break;
case "description":
description = readText(xpp);
break;
case "buy":
buy = readText(xpp);
break;
case "module":
module = readText(xpp);
break;
}
}
mDbHelper.createBooks(author,title, price, publish_date, description, module, buy);

}
}
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
}


private String readText(XmlPullParser parser) throws IOException,
XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
@Override
protected void onDestroy() {
super.onDestroy();

if (mDbHelper != null && mDbHelper == mDbHelper.open()){
mDbHelper.close();
}
}


@Override
public boolean onClose() {
//myList.setAdapter(defaultAdapter);
displayResults("");
return false;
}


@Override
public boolean onQueryTextChange(String newText) {
// if (!newText.isEmpty()){
displayResults(newText + "*");
// } else {
// myList.setAdapter(defaultAdapter);
///}
return false;
}
@Override
public boolean onQueryTextSubmit (String query) {
displayResults(query + "*");
return false;
}

//search and results method
private void displayResults(String query) {

Cursor cursor = mDbHelper.searchBooks((query != null ? query : "@@@@"));

if (cursor != null) {

String[] from = new String[] {
BooksDBAdapter.KEY_AUTHOR,
BooksDBAdapter.KEY_TITLE,
BooksDBAdapter.KEY_MODULE,
};

//view we want to set results
int[] to = new int[] {R.id.author, R.id.title, R.id.module};
//create cursor adapter. which exposes data from a Cursor to a ListView
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.search_results, cursor, from, to, 0);
myList.setAdapter(cursorAdapter);

//listview Click listener for selected results

//WAS PREVIOUSLY - myList.setOnItemClickListener(new AdapterView.OnItemClickListener()
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) myList.getItemAtPosition(position);

String author = cursor.getString(cursor.getColumnIndexOrThrow("author"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String price = cursor.getString(cursor.getColumnIndexOrThrow("price"));
String publish_date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
String description = cursor.getString(cursor.getColumnIndexOrThrow("description"));
String module = cursor.getString(cursor.getColumnIndexOrThrow("module"));
String buy = cursor.getString(cursor.getColumnIndexOrThrow("buy"));

//Check if the Layout already exists
LinearLayout bookLayout = (LinearLayout)findViewById(R.id.customerLayout);
if(bookLayout == null){
//Inflate the Customer Information Vie
LinearLayout xbookLayout = (LinearLayout)findViewById(R.id.Layout);

View book = getLayoutInflater().inflate(R.layout.book_info, xbookLayout, false);
xbookLayout.addView(book);
}

//Get References to the TextViews
authorText = (TextView) findViewById(R.id.xauthor);
titleText = (TextView) findViewById(R.id.xtitle);
priceText = (TextView) findViewById(R.id.xprice);
publishDateText = (TextView) findViewById(R.id.xpublish_date);
descriptionText = (TextView) findViewById(R.id.xdescription);
moduleText = (TextView) findViewById(R.id.xmodule);
buyText = (TextView) findViewById(R.id.xbuy);

// Update the parent class's TextView
authorText.setText(author);
titleText.setText(title);
priceText.setText(price);
publishDateText.setText(publish_date);
descriptionText.setText(description);
moduleText.setText(module);

String text ="<a href ="+buy+">Click to buy</a>";
buyText.setMovementMethod(LinkMovementMethod.getInstance());
buyText.setText(Html.fromHtml(text));
saveB = (Button) findViewById(R.id.save);
saveB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View button) {

saveBook(button);
}
});

searchView.setQuery("",true);

}
});
}

}




public void saveBook(View button) {
String s1 = authorText.getText().toString();
String s2 = titleText.getText().toString();
String s3 = priceText.getText().toString();
String s4 = publishDateText.getText().toString();
String s5 = descriptionText.getText().toString();
String s6 = moduleText.getText().toString();
String s7 = buyText.getText().toString();


mDbHelper.saveBooks(s1, s2, s3, s4, s5, s6, s7);
}
}


Database



public class BooksDBAdapter {

public static final String KEY_AUTHOR = "author";
public static final String KEY_TITLE = "title";
public static final String KEY_PRICE= "price";
public static final String KEY_PUBLISH_DATE = "date";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_MODULE = "module";
public static final String KEY_BUY = "buy";
public static final String KEY_SEARCH = "searchData";

private static final String TAG = "BooksDBAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DATABASE_NAME = "BooksData";
private static final String FTS_VIRTUAL_TABLE = "FTSBooksData";
private static final String FTS_VIRTUAL_TABLE_2 = "FTSSavedBookData";

private static final int DATABASE_VERSION = 6;

//Create FTS3 virtual table to provide a more robust and faster serarch
private static final String DATABASE_CREATE =
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
KEY_AUTHOR + " ," +
KEY_TITLE + " ," +
KEY_PRICE + " ," +
KEY_PUBLISH_DATE + " ," +
KEY_DESCRIPTION + " ," +
KEY_MODULE + " ," +
KEY_BUY + " ," +
KEY_SEARCH + " ," +
" UNIQUE (" + KEY_TITLE + "));";

private static final String DATABASE_CREATE_2 =
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE_2 + " USING fts3(" +
KEY_AUTHOR + " ," +
KEY_TITLE + " ," +
KEY_PRICE + " ," +
KEY_PUBLISH_DATE + " ," +
KEY_DESCRIPTION + " ," +
KEY_MODULE + " ," +
KEY_BUY + " ," +
" UNIQUE (" + KEY_DESCRIPTION + "));";


private final Context context;

private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE_2);
onCreate(db);
}
}

public BooksDBAdapter(Context ctx) {
this.context = ctx;
}

//when we open the db
public BooksDBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(context);
mDb = mDbHelper.getWritableDatabase();
return this;
}

public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}

public long createBooks( String author, String title, String price, String publish_date, String description, String module, String buy) {

ContentValues initialValues = new ContentValues();
String searchValue =
author + " " +
title + " " +
price + " " +
publish_date + " " +
module + " " +
buy;
initialValues.put(KEY_AUTHOR, author);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PRICE, price);
initialValues.put(KEY_PUBLISH_DATE, publish_date);
initialValues.put(KEY_DESCRIPTION, description);
initialValues.put(KEY_MODULE, module);
initialValues.put(KEY_BUY, buy);
initialValues.put(KEY_SEARCH, searchValue);

return mDb.insert(FTS_VIRTUAL_TABLE, null, initialValues);
}


public Cursor searchBooks(String inputText) throws SQLException {
Log.w(TAG, inputText);
String query = "SELECT docid as _id," +
KEY_AUTHOR + "," +
KEY_TITLE + "," +
KEY_PRICE + "," +
KEY_PUBLISH_DATE + "," +
KEY_DESCRIPTION + "," +
KEY_MODULE + "," +
KEY_BUY +
" from " + FTS_VIRTUAL_TABLE +
" where " + KEY_SEARCH + " MATCH '" + inputText + "';";

Log.w(TAG, query);
Cursor mCursor = mDb.rawQuery(query, null);

if (mCursor != null ) {
mCursor.moveToFirst();
//mCursor.close();
}
return mCursor;

}

public boolean deleteAllBooks() {
int doneDelete;

doneDelete = mDb.delete(FTS_VIRTUAL_TABLE, null , null) + mDb.delete(FTS_VIRTUAL_TABLE_2, null , null);

return doneDelete > 0;


}

public long saveBooks( String author, String title, String price, String publish_date, String description, String module, String buy) {

ContentValues initialValues = new ContentValues();

initialValues.put(KEY_AUTHOR, author);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PRICE, price);
initialValues.put(KEY_PUBLISH_DATE, publish_date);
initialValues.put(KEY_DESCRIPTION, description);
initialValues.put(KEY_MODULE, module);
initialValues.put(KEY_BUY, buy);

return mDb.insert(FTS_VIRTUAL_TABLE_2, null, initialValues);
}
}


I've spent almost two days trying to work out what i'm doing wrong, this functionality shouldn't be too hard to implement?


Aucun commentaire:

Enregistrer un commentaire