单击按钮更新SQlite数据库中的行

时间:2018-12-22 11:56:12

标签: java android sqlite nullpointerexception android-sqlite

我有一个按钮,当我单击它时,我想在SQLite中将行的字段设置为1。该行的默认值为0。我要传递ID,然后更新ID的Liked行。我写了如下的更新方法:

argv[2]

这是我的recyclerView Adapter类。

public void setLiked(long id) {
    walldb = this.getWritableDatabase() ;
    ContentValues values = new ContentValues();
    values.put(Colwall_Liked,1);
    walldb.update(TABLE_NAME , values, "=" + id,null) ;
    walldb.close();
}

我在recyclerview Adapter的viewholder类中编写了onClick方法。

public class wallAdapter extends RecyclerView.Adapter<wallAdapter.ViewHolderWall> {

private Context ctx;
List<ModelWallpaper> modelWallpaper;
WallpaperHelper myHelper;

public wallAdapter(Context ctx, List<ModelWallpaper> modelWallpaper) {
    this.ctx = ctx;
    this.modelWallpaper = modelWallpaper;
}

@NonNull
@Override
public ViewHolderWall onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(ctx).inflate(R.layout.recyclerview_wallpaper,viewGroup,false);
    return new wallAdapter.ViewHolderWall(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolderWall viewHolderWall, int i) {
    final ModelWallpaper model = modelWallpaper.get(i);

    Glide.with(ctx)
            .load(model.getImageURL())
            .apply(new RequestOptions().centerCrop())
            .into(viewHolderWall.image);
    viewHolderWall.id = model.getID() ;

    viewHolderWall.setWall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Glide.with(ctx)
                    .asBitmap()
                    .load(model.getImageURL())
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                            try {
                                WallpaperManager.getInstance(ctx).setBitmap(resource) ;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

        }
    });



}

@Override
public int getItemCount() {
    return modelWallpaper.size();
}

class ViewHolderWall extends RecyclerView.ViewHolder {

private ImageView image;
private ShineButton heart;
private Button setWall;
long id;


public ViewHolderWall(View itemView) {
    super(itemView);

    image = itemView.findViewById(R.id.wallimage) ;
    heart = itemView.findViewById(R.id.po_image1);
    setWall = itemView.findViewById(R.id.setWall);

    final ModelWallpaper modelWallpaper = new ModelWallpaper();

    heart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(modelWallpaper.getLiked() == 0) {
                myHelper.setLiked(id);
            }
        }
    });
}
}


}

这是数据库Hepler类

long id; 

final ModelWallpaper modelWallpaper = new ModelWallpaper();

heart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(modelWallpaper.getLiked() == 0) {
                myHelper.setLiked(id);
            }
        }
    });

但是当我单击按钮时,应用程序强制停止。这是我的日志

public class WallpaperHelper extends SQLiteOpenHelper{


SQLiteDatabase walldb ;

private static final String DATABASE_NAME = "Wallpaper.db" ;
private static final String TABLE_NAME = "Wallpaper_table" ;
private static final String Colwall_id = "ID" ;
private static final String Colwall_Url = "URL" ;
private static final String Colwall_Liked = "Liked" ;

public WallpaperHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}


@Override
public void onCreate(SQLiteDatabase db) {
    String query = "create table if not exists " + TABLE_NAME + "(" + Colwall_id + " integer primary key autoincrement , " + Colwall_Url + " text not null , " + Colwall_Liked + " integer default 0" + ")";

    try {
        db.execSQL(query);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    onCreate(db);
}

1 个答案:

答案 0 :(得分:0)

myHelper = new WallpaperHelper(ctx)内初始化ViewHolderWall