mercredi 15 avril 2015

Saving Textview into Sqlite

Hey I saw you solved a similar problem. I cannot seem to get a response to the button.


What I want is the stored textview to be inserted into the SaveBook table in my database. I'm sure I have got the database set up right and the button set up right, So i'm struggling to see where I am going wrong. I am getting no response from the button.


Here is my button XML.



<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saveButton"
android:id="@+id/save"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />


My 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 values = new ContentValues();

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

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


My Button method:



public void onClick(View view) {
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);
}


Where the texviews are retrieved from



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() {


searchView.setQuery("", true);



}

});

}

}

Aucun commentaire:

Enregistrer un commentaire