jeudi 22 octobre 2015

Adding Delete Button to listview

I managed to create a customise list view connected to sql db. now i want to add a remove button to it. I already managed to turn on delete function when i long press a row using this code:

     private void listViewItemLongClick(){
    ListView MyList = (ListView)findViewById(R.id.listView);
    MyList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
            dbHandler.deleteExpense(id); 
            populateListView();
            return false;
        }


    });

now i added a button to my listview to do the same thing but cant manage to make it working. this is the the layout xml:

   <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView4"
    android:clickable="true"
    android:enabled="true"
    android:ellipsize="start" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/idTextView"
    android:paddingRight="15dp"
    android:enabled="true"
    android:ellipsize="start" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/categroyTextView"
    android:paddingRight="15dp"
    android:enabled="true"
    android:ellipsize="start" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/amoutTextView"
    android:paddingRight="15dp"
    android:enabled="true"
    android:ellipsize="start" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="delete"
    android:id="@+id/delete_button"
    android:enabled="true"
    android:ellipsize="start" />

and my db handler:

  public class MyDBHandler extends SQLiteOpenHelper {
  private static final int DataBase_Version=1;
private static final String DATABASE_NAME = "expenses.db";
public static final String TABLE_EXPENSES= "expenses";
public static final String COLUMN_ID="_id";
public static final String COLUMN_CATEGORY="category";
public static final String COLUMN_AMOUNT="amount";
public static final String[] ALL_KEYS = new String[]{COLUMN_ID,COLUMN_CATEGORY,COLUMN_AMOUNT};
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_EXPENSES + "(" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_CATEGORY + " TEXT, " +
        COLUMN_AMOUNT + " TEXT  " +
        ");";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_EXPENSES);
    onCreate(db);
}

//add a new row to the db
public void addExpense(Expense expense){
    ContentValues values = new ContentValues();
    values.put(COLUMN_CATEGORY,expense.get_category());
    values.put(COLUMN_AMOUNT,expense.get_amount());
    SQLiteDatabase db =getWritableDatabase();
    db.insert(TABLE_EXPENSES, null, values);
    db.close();


}

//delete a row
public void deleteExpense(long id){
    SQLiteDatabase db =getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_EXPENSES + " WHERE " + COLUMN_ID + "=\"" + id + "\";" );
}

//print out db

public String databaseToString(){
    String dbString="";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_EXPENSES + " WHERE 1";
    Cursor c = db.rawQuery(query,null);
    c.moveToFirst();
    while (!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("category"))!= null) {
            dbString += c.getString(c.getColumnIndex("_id"))+" ";
            dbString += c.getString(c.getColumnIndex("category"))+" ";
            dbString += c.getString(c.getColumnIndex("amount"))+ "NIS"  ;
            dbString += "\n";
            c.moveToNext();
        }
        }
    db.close();
    return dbString;
    }

public Cursor getAllRows(){
    SQLiteDatabase db = getWritableDatabase();
    String where=null;
    //Cursor c = db.query(true,TABLE_EXPENSES,ALL_KEYS,where,null,null,null);
    Cursor c =  db.query(true, TABLE_EXPENSES, ALL_KEYS,
            where, null, null, null, null, null);
    if(c!=null)
        c.moveToFirst();
    return c;
}
}

thank you guys

Aucun commentaire:

Enregistrer un commentaire