提问人:Unicorn 提问时间:12/22/2018 最后编辑:PhantômaxxUnicorn 更新时间:12/22/2018 访问量:472
使用按钮单击[复制]更新SQlite数据库中的行
Update a row in SQlite database With Button Click [duplicate]
问:
我有一个按钮,当我单击它时,我想将SQLite中的行字段设置为1。该行的默认值为 0。我想传递 id,然后更新 id 的 Liked 行。我写了更新方法如下:
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 类。
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);
}
}
});
}
}
}
我在 recyclerview Adapter 的 viewholder 类中编写了 onClick 方法。
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);
}
}
});
这是数据库 Hepler 类
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);
}
但是当我单击按钮时,应用程序强制停止。这是我的日志
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.hp.finalproject.Activities.Wallpaper.WallpaperHelper.setLiked(long)' on a null object reference
at com.example.hp.finalproject.Activities.Wallpaper.wallAdapter$ViewHolderWall$1.onClick(wallAdapter.java:112)
at com.sackcentury.shinebuttonlib.ShineButton$OnButtonClickListener.onClick(ShineButton.java:343)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答:
0赞
EL TEGANI MOHAMED
12/22/2018
#1
在myHelper = new WallpaperHelper(ctx)
ViewHolderWall
评论
0赞
Unicorn
12/22/2018
哇,它奏效了。你拯救了我的一天。谢谢
0赞
EL TEGANI MOHAMED
12/22/2018
很高兴它帮助了你是惠康
评论