如何通过 switch compact 将值 1 插入 sq-lite 中的列
Posted
技术标签:
【中文标题】如何通过 switch compact 将值 1 插入 sq-lite 中的列【英文标题】:How to insert value 1 into a column in sq-lite through switch compact 【发布时间】:2020-03-01 16:57:13 【问题描述】:当我在 recyclerView 内的卡片视图中单击 switch compact 时,我想在 SQLite 列中插入值 1 ..但是当我单击 switchCompact 时出现 NullPointeException。 enter image description here 在此先感谢.. 我是 android 新手
orderListAdapter.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);
);
DatabaseHelper.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);
logcat :-
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)
【问题讨论】:
【参考方案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 中发布错误
【讨论】:
首先感谢您的回复,很抱歉您的建议不起作用。我将发布我的logcat,希望您能帮助我找到解决方案【参考方案2】:首先不要在适配器上做数据库相关的工作,我会为你提供解决方案,你可以在片段上的活动上做数据库相关的工作。 首先在您的适配器中创建一个接口,
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;
向您的活动/片段发送回调,
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position)
final orderbook orderbook = orderbookList.get(position);
if (interaction != null)
interaction.onWorkComplete(position, orderbook);
在活动/片段中初始化您的适配器,
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
【讨论】:
以上是关于如何通过 switch compact 将值 1 插入 sq-lite 中的列的主要内容,如果未能解决你的问题,请参考以下文章
如何定制Windows Embedded Compact 7 shell详解
如何通过 JDBC 将值插入 Microsoft Access 数据库?