joehamirbalabadan.com joehamirbalabadan.com - 4 months ago 22
Android Question

How to update sqlite listview item from adapter?

please help me.

i have

ListView
with
ImageButton
,

when i click the
ImageButton
, i want to update the field of the item.

here is my adapter bindView

@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView txtTitle = (TextView) view.findViewById(R.id.title);
TextView txtArtist = (TextView) view.findViewById(R.id.artist);
TextView txtVolume = (TextView) view.findViewById(R.id.volume);
TextView txtNumber = (TextView) view.findViewById(R.id.number);

ImageButton buttonHeart = (ImageButton) view.findViewById(R.id.heart);


final int _id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String artist = cursor.getString(cursor.getColumnIndexOrThrow("artist"));
String volume = cursor.getString(cursor.getColumnIndexOrThrow("volume"));
final String favorite = cursor.getString(cursor.getColumnIndexOrThrow("favorite"));
String number = cursor.getString(cursor.getColumnIndexOrThrow("number"));

// Populate fields with extracted properties
txtTitle.setText(title);
txtArtist.setText(artist);
txtVolume.setText(volume);
txtNumber.setText(number);

if(favorite==null) {
buttonHeart.setImageResource(R.drawable.heart);
}else if(favorite.matches("1")){
buttonHeart.setImageResource(R.drawable.heartred);
}

buttonHeart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (arg0 != null) {
//UPDATE QUERY HERE
}
}
});

}


and here is my DbAdapter

package com.magicstarme.virtualsongbook;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


/**
* Created by Joe on 7/7/2016.
*/
public class FragmentOne_DbAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_ARTIST = "artist";
public static final String KEY_VOLUME = "volume";
public static final String KEY_TYPE = "type";
public static final String KEY_FAVORITE = "favorite";
public static final String KEY_NUMBER = "number";

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

private static final String DATABASE_NAME = "Virtualsongbook";
private static final String SQLITE_TABLE = "Player1";
private static final int DATABASE_VERSION = 1;

private final Context mCtx;

private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_TITLE + "," +
KEY_ARTIST + "," +
KEY_VOLUME + "," +
KEY_TYPE + "," +
KEY_FAVORITE + "," +
KEY_NUMBER + ")";

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);
}

@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 " + SQLITE_TABLE);
onCreate(db);
}
}

public FragmentOne_DbAdapter(Context ctx) {
this.mCtx = ctx;
}

public FragmentOne_DbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}

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

public long createPlayer1(String title,
String artist, String volume,
String type, String number) {

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_ARTIST, artist);
initialValues.put(KEY_VOLUME, volume);
initialValues.put(KEY_TYPE, type);
initialValues.put(KEY_NUMBER, number);

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

public boolean deleteAllPlayer1() {

int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;

}

public Cursor fetchPlayer1ByTitle(String titleText) throws SQLException {
Log.w(TAG, titleText);
Cursor mCursor = null;
if (titleText == null || titleText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_ARTIST, KEY_VOLUME, KEY_TYPE, KEY_FAVORITE, KEY_NUMBER},
null, null, null, null, null);

}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_ARTIST, KEY_VOLUME, KEY_TYPE, KEY_FAVORITE, KEY_NUMBER},
KEY_TITLE + " like '%" + titleText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;

}

public Cursor fetchAllPlayer1() {

Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_ARTIST, KEY_VOLUME, KEY_TYPE, KEY_FAVORITE, KEY_NUMBER},
null, null, null, null, null);

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

public void insertPlayer1Songlist() {

createPlayer1("Song Title 1", "Artist 1", "Vol 1","","12341");
createPlayer1("Song Title 2", "Artist 2", "Vol 2","","12342");
createPlayer1("Song Title 3", "Artist 3", "Vol 2","","12343");
createPlayer1("Song Title 4", "Artist 4", "Vol 3","","12344");
createPlayer1("Song Title 5", "Artist 5", "Vol 3","","12345");
createPlayer1("Song Title 6", "Artist 6", "Vol 4","","12346");
createPlayer1("Joe Song Title 7", "Artist 7", "Vol 4","","12347");
createPlayer1("Song Title 8", "Artist 8", "Vol 1","","12348");
createPlayer1("Song Title 9", "Artist 9", "Vol 1","","12349");
createPlayer1("Song Title 1", "Artist 1", "Vol 1","","12341");
createPlayer1("Song Title 2", "Artist 2", "Vol 2","","12342");
createPlayer1("Song Title 3", "Artist 3", "Vol 2","","12343");
createPlayer1("Song Title 4", "Artist 4", "Vol 3","","12344");
createPlayer1("Song Title 5", "Artist 5", "Vol 3","","12345");
createPlayer1("Song Title 6", "Artist 6", "Vol 4","","12346");
createPlayer1("Song Title 7", "Artist 7", "Vol 4","","12347");
createPlayer1("Song Title 8", "Artist 8", "Vol 1","","12348");
createPlayer1("Song Title 9", "Artist 9", "Vol 1","","12349");
createPlayer1("Joehamir Balabadan", "Artist 1", "Vol 1","","12341");
createPlayer1("Song Title 2", "Artist 2", "Vol 2","","12342");
createPlayer1("Song Title 3", "Artist 3", "Vol 2","","12343");
createPlayer1("Song Title 4", "Artist 4", "Vol 3","","12344");
createPlayer1("Song Title 5", "Artist 5", "Vol 3","","12345");
createPlayer1("Song Title 6", "Artist 6", "Vol 4","","12346");
createPlayer1("Song Title 7", "Artist 7", "Vol 4","","12347");
createPlayer1("Song Title 8", "Artist 8", "Vol 1","","12348");
createPlayer1("Song Title Joe 9", "Artist 9", "Vol 1","","12349");

}
}


when i click
ImageButton
, i want to update favorite field from null to 1
then when favorite field is 1, i want to update is to null.

also i want to change imageResource when i click
ImageButton
..

from heart to hearthred and vise versa.

please help.

thanks,

joe

Answer

1> add this method in FragmentOne_DbAdapter

public int updateItemFavorite(int rowId, String favorite) {
    ContentValues values = new ContentValues();
    values.put(KEY_FAVORITE, favorite);
    return mDb.update(SQLITE_TABLE, values, KEY_ROWID + " = ?", new String[] { String.valueOf(rowId) });
}

2> replace this code

if(favorite==null) {
        buttonHeart.setImageResource(R.drawable.heart);
    }else if(favorite.matches("1")){
        buttonHeart.setImageResource(R.drawable.heartred);
    }

with

 if(favorite.matches("0")) {
        buttonHeart.setImageResource(R.drawable.heart);
    }else if(favorite.matches("1")){
        buttonHeart.setImageResource(R.drawable.heartred);
    }

3> call updateItemFavorite image button click

buttonHeart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (arg0 != null) {
                FragmentOne_DbAdapter database=new FragmentOne_DbAdapter(context);

  if(favorite.matches("0")) {
        database.updateItemFavorite(_id,"1");
    }else if(favorite.matches("1")){
        database.updateItemFavorite(_id,"0");
    }

adapter.notifyDataSetChanged();//here adapter is my adapter class object

            }
        }
    });

i hope it useful to you