如何从适配器更新sqlite listview项?

时间:2016-07-08 03:48:47

标签: android sqlite listview

ImageButtonImageButton

当我点击@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 } } }); } 时,我想更新该项目的字段。

这是我的适配器bindView

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

    }
}

这是我的DbAdapter

ImageButton

当我点击ImageButton时,我想将收藏的字段从null更新为1 然后当收藏字段为1时,我想更新为null。

我也想在点击{{1}} ..

时更改imageResource 从心到心,反之亦然。

1 个答案:

答案 0 :(得分:0)

1>在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 - ;替换此代码

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

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

3>调用updateItemFavorite图像按钮单击

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(); //此处适配器是我的适配器类对象

            }
        }
    });

我希望它对你有用