将复选框状态保存到listview中的sqlite

时间:2017-05-03 04:18:37

标签: android sqlite listview checkbox

我有问题。在我的项目中,我使用现有的sqlite数据库填充listview。我想在listview中添加一个复选框,它可以保存复选框状态并使用保存和重新加载按钮重新加载状态。

我已经为我的数据库中的复选框添加了字段,我正在尝试将其保存为1&由于sqlite中没有布尔值,因此检查是否表示0表示复选框。我的问题是我无法找到一种方法将复选框保存到我的数据库并重新加载它。

我正在尝试实现此代码:

    CheckBox cb = (CheckBox) v.findViewById(R.id.tv_checked);

    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                checked = 1;
            } else {
                checked = 0;
            }
        }
    });

它适用于其他情况但我似乎不知道如何在我的listview中使用此代码。

这是我的代码。

PreDive.java:

public class PreDive extends Activity {
private ListView lvProduct;
private ListAdapter adapter;
private List<Checklist_Info> mProductList;
private DatabaseHelper mDBHelper;

Button Save, Reload;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.check_listview);
    lvProduct = (ListView) findViewById(R.id.listview_product);
    mDBHelper = new DatabaseHelper(this);
    Save = (Button)findViewById(R.id.button1);
    Reload = (Button)findViewById(R.id.button2);


    //Check exists database
    File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
    if (!database.exists()) {
        mDBHelper.getReadableDatabase();
        //Copy db
        if (copyDatabase(this)) {
            Toast.makeText(this, "Copy database success", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Copy data error", Toast.LENGTH_SHORT).show();
            return;
        }
    }
    mProductList = mDBHelper.getListProduct();
    adapter = new ListAdapter(this, mProductList);
    lvProduct.setAdapter(adapter);
}


private boolean copyDatabase(Context context) {
    try {

        InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
        String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
        OutputStream outputStream = new FileOutputStream(outFileName);
        byte[]buff = new byte[1024];
        int length;
        while ((length = inputStream.read(buff)) > 0) {
            outputStream.write(buff, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        Log.w("MainActivity","DB copied");
        return true;
    }catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

ListAdapter.java:

public class ListAdapter extends BaseAdapter {
private final Activity mContext;
private List<Checklist_Info> mProductList;


public ListAdapter(Activity mContext, List<Checklist_Info> mProductList) {
    this.mContext = mContext;
    this.mProductList = mProductList;
}



@Override
public int getCount() {
    return mProductList.size();
}

@Override
public Object getItem(int position) {


    return mProductList.get(position);
}

@Override
public long getItemId(int position) {
    return mProductList.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    View v = View.inflate(mContext, R.layout.check_row, null);

    TextView tvArea = (TextView)v.findViewById(R.id.tv_area);
    TextView tvType = (TextView)v.findViewById(R.id.tv_type);
    TextView tvDesc = (TextView)v.findViewById(R.id.tv_desc);
    tvArea.setText(mProductList.get(position).getArea());
    tvType.setText(mProductList.get(position).getType());
    tvDesc.setText(mProductList.get(position).getDesc());

    return v;
}

DatabaseHelper:

    public List<Checklist_Info> getListProduct() {
    Checklist_Info checklist_info;
    List<Checklist_Info> productList = new ArrayList<>();
    openDatabase();
    Cursor cursor = mDatabase.rawQuery("SELECT * FROM PREDIVE", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        checklist_info = new Checklist_Info(cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                cursor.getString(3), cursor.getString(4));
        productList.add(checklist_info);
        cursor.moveToNext();
    }
    cursor.close();
    closeDatabase();
    return productList;
}

0 个答案:

没有答案
相关问题