samedi 31 octobre 2015

How to display listview by using SQLiteDatabase Android

I have been learning SQLiteDatabase b y refering to thenewboston tuts. I want to modify somethings in my app.

Here, when I add data it covers up the screen and doesn't look oraganied. I would like to add a listview and store these items in the listview only.

Looks like this, I don't like it

So I would like to represent it like this

enter image description here

All my items should go in that listview item box only and i don't what all variable i should tell adapter to fetch from my db file or product file.

Any help would be appreciated ! thanks !

here is my code [MainActivity]:

EditText buckysInput;
TextView buckysText;
MyDBHandler dbHandler;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buckysInput = (EditText) findViewById(R.id.buckysInput);
    buckysText = (TextView) findViewById(R.id.buckysText);
    dbHandler = new MyDBHandler(this, null, null, 1);
    printDatabase();
}

//Add a product to the database

public void addButtonClicked(View view){
    Product product = new Product(buckysInput.getText().toString());
    dbHandler.addProduct(product);
    printDatabase();
}

//Delete items

public void deleteButtonClicked(View view)
{
    String inputText = buckysInput.getText().toString();
    dbHandler.deleteProduct(inputText);
    printDatabase();
}

public void formatButtonClicked(View view)

{

    dbHandler.formatDatabase();
    buckysText.setText("");


    Toast.makeText(getApplicationContext(),"deleted successfully", Toast.LENGTH_SHORT).show();



}

//Print the database

public void printDatabase()

{
    String dbString = dbHandler.databaseToString();
    buckysText.setText(dbString);
    buckysInput.setText("");
}

}

MyDBHandler

 private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "product1DB.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";

//We need to pass database information along to superclass
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 TABLE IF EXISTS " + TABLE_PRODUCTS);
    onCreate(db);
}

//Add a new row to the database
public void addProduct(Product product){
    ContentValues values = new ContentValues();
    values.put(COLUMN_PRODUCTNAME, product.get_productname());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
}

//Delete a product from the database
public void deleteProduct(String productName){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}



public String databaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

    //Cursor points to a location in your results
    Cursor c = db.rawQuery(query, null);
    //Move to the first row in your results
    c.moveToFirst();

    //Position after the last row means the end of the results
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("productname")) != null) {
            dbString += c.getString(c.getColumnIndex("productname"));
            dbString += "\n";
        }
        c.moveToNext();






    }
    db.close();
    return dbString;
}


//Delete the whole database HAHAHA !!
public void formatDatabase()

{
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("delete from " + TABLE_PRODUCTS);


}

}

Product.java

private int _id;
private String _productname;

public Product(){
}

public Product(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