提问人:Ziyad Rahman 提问时间:11/5/2019 最后编辑:Ziyad Rahman 更新时间:11/7/2019 访问量:47
如何通过 switch compact 将值 1 插入到 sq-lite 中的列中
How to insert value 1 into a column in sq-lite through switch compact
问:
我想在 recyclerView 内的卡片视图中单击开关紧凑时在 SQLite 列中插入值 1。但是当我单击switchCompact时,得到NullPointeException。在此处输入图像描述提前致谢..我是 Android 新手
订单列表适配器 .java
public void onBindViewHolder(@NonNull final orderListHolder holder, final int position) {
final orderbook orderbook = orderbookList.get(position);
holder.orderno.setText(orderbook.orderNo + " ");
holder.customerName.setText(orderbook.customerName);
holder.itemName.setText(orderbook.itemName);
holder.workCompleteSwicth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(holder.workCompleteSwicth.isChecked()) {
myDb.InsertWorkComplete(1);
}
}
});
数据库助手.java
public void InsertWorkComplete( int isWorkComplete)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COLUMN_ISWORKCOMPLETE,isWorkComplete);
db.update(TABLE_NAME, contentValues, null, null);
}
日志猫 :-
Process: com.example.android.saffrondesigner, PID: 5359
java.lang.NullPointerException
at com.example.android.saffrondesigner.orderListAdapter$1.onCheckedChanged(orderListAdapter.java:60)
at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
at androidx.appcompat.widget.SwitchCompat.setChecked(SwitchCompat.java:1060)
at androidx.appcompat.widget.SwitchCompat.toggle(SwitchCompat.java:1055)
at android.widget.CompoundButton.performClick(CompoundButton.java:99)
at android.view.View$PerformClick.run(View.java:18797)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
答:
0赞
Ramadanrizky87
11/5/2019
#1
在这里
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(holder.workCompleteSwicth.isChecked()) {
myDb.InsertWorkComplete(1);
}
我想只要改成holder.workCompleteSwicth.isChecked()
`public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b) {
myDb.InsertWorkComplete(1);
//u can setChecked of switch her ex: model.setChecked(true);
}`
如果它不起作用,您可以在 Logcat 中发布错误吗
评论
0赞
Ziyad Rahman
11/7/2019
首先感谢您的回复,很抱歉说您的建议不起作用。我会发布我的logcat,希望你能帮我找到解决方案
0赞
Kintan Patel
11/5/2019
#2
首先,不要在适配器上做数据库相关的工作,我将为您提供解决方案,您可以在 Fragment 上的 Activity 上做与数据库相关的工作。 首先在适配器中创建一个接口,
interface WorkCompleteInteraction {
void onWorkComplete(int position, OrderBook orderbook);
}
在构造函数中获取接口,
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private WorkCompleteInteraction interaction;
private Context mContext;
public MyAdapter(WorkCompleteInteraction interaction, Context mContext) {
this.interaction = interaction;
this.mContext = mContext;
}
向您的 activity/fragment 发送回调,
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final orderbook orderbook = orderbookList.get(position);
if (interaction != null) {
interaction.onWorkComplete(position, orderbook);
}
}
在 activity/fragment 中初始化适配器,
MyAdapter adapter = new MyAdapter(this,this);
在您的活动中实现您的接口,
public class TestActivity extends AppCompatActivity implements MyAdapter.WorkCompleteInteraction {
最后覆盖你的方法,
@Override
public void onWorkComplete(int position, OrderBook orderbook) {
//do your database related work here
}
评论