I have a problem. Here in my project, I populate my listview with an existing sqlite database. I want to add a checkbox to my listview and it can save the checkbox state and reload back the state with save and reload buttons.
I've already add the field for the checkbox in my database and I'm trying to save it as 1 & 0 to represent checkbox is checked or not as there's no boolean in sqlite. My problem is I cant find a way to save the checkbox to my db and reload it.
Im trying to implement this code:
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;
}
}
});
It work on other cases but I cant seems to know how to use this code in my listview.
This is my code.
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;
}
I didnt show my dbhelper class. but if you want to see it tell me an I'll put it
Aucun commentaire:
Enregistrer un commentaire