This is a simple database app which stores something in a table(on button click) and deletes(on button click). On installing the app on device(lollipop), main activity pops up, but as soon as I enter some text in the textfield and click the button to save it to database, the app freezes and force closes after some time.Also if I try running the app again then the main activity doesn't start and the app force closes.
This is the main activity,
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText myinput;
TextView mytext;
MyDBHandler dbhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myinput =(EditText) findViewById(R.id.myinput);
mytext =(TextView) findViewById(R.id.mytext);
dbhandler= new MyDBHandler(this, null, null, 1);
printDatabase();
}
//add product to database
public void addstuff(View view)
{
products product = new products(myinput.getText().toString());
dbhandler.addProduct(product);
printDatabase();
}
//delete items from database
public void deletestuff(View view)
{
String inputtext= myinput.getText().toString();
dbhandler.deleteProduct(inputtext);
printDatabase();
}
public void printDatabase()
{
String dbString= dbhandler.databaseToString();
mytext.setText(dbString);
myinput.setText("");
}
}
This is the database handler class
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID ="_id";
public static final String COLUMN_PRODUCTNAME ="productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query= "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TALE IF EXISTS "+TABLE_PRODUCTS);
onCreate(db);
}
//add a new row to the database
public void addProduct(products product)
{
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
SQLiteDatabase db= getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
//delete product from the database
public void deleteProduct(String productname)
{
SQLiteDatabase db= getWritableDatabase();
db.execSQL("DELETE FROM "+ TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " =\"" + productname + "\";" );
}
//printing out the database as a string
public String databaseToString()
{
String dbString ="";
SQLiteDatabase db= getWritableDatabase();
String query = "SELECT * FROM "+ TABLE_PRODUCTS + " WHERE 1";
//CURSOR POINTS TO A LOCATION IN THE DATABASE RESULTS
Cursor c= db.rawQuery(query,null);
//go to 1st row in your results
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("productname"))!=null)
{
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
this is the products class which I'v used for setting and getting names
public class products {
private int _id;
private String _productname;
public products()
{
}
public products(String productname) {
this._productname = productname;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_productname(String _productname) {
this._productname = _productname;
}
public int get_id() {
return _id;
}
public String get_productname() {
return _productname;
}
}
Aucun commentaire:
Enregistrer un commentaire