I am using Checkbox with each and every list item, and when user do tap on any of the checkbox i am storing that list item into SQLite database, but whenever i do restart my app not getting check for list items those i checked earlier.
So How do I save state to Checkbox ?
Here is my code:
public class ServiceAdapter extends BaseAdapter {
ArrayList<Service> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Context context;
int i = 0;
long saveData;
MainActivity activity;
public ServiceAdapter(Context context, int resource, ArrayList<Service> objects, MainActivity activity) {
this.context= context;
this.vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.Resource = resource;
this.actorList = objects;
this.activity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvName = (TextView) v.findViewById(R.id.textView1);
holder.checkBox = (CheckBox) v.findViewById(R.id.cbBox);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.tvName.setText(actorList.get(position).getName());
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
String strName = actorList.get(position).getName().toString();
if (contains(DetailArrayList.detailArraylist, strName)) {
System.out.println("Data Exist(s)");
Toast.makeText(context, strName+" Already exist(s)", Toast.LENGTH_LONG).show();
} else {
System.out.println("Not Exist(s)");
DetailArrayList.detailArraylist.add(0, new Detail(actorList.get(position).getName()));
saveData = activity.myDb.InsertData(actorList.get(position).getName());
i = DetailArrayList.detailArraylist.size();
Log.d("detail list size:", String.valueOf(i));
Toast.makeText(context, strName+" Added to Service Details", Toast.LENGTH_LONG).show();
}
}
}
private boolean contains(ArrayList<Detail> cartArraylist,
String strName) {
// TODO Auto-generated method stub
for (Detail item : cartArraylist) {
if (item.getName().equals(strName)) {
return true;
}
}
return false;
}
});
return v;
}
static class ViewHolder {
public TextView tvName;
public CheckBox checkBox;
}
Database class:
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(ServiceName VARCHAR(100));"); // 1
Log.d("CREATE TABLE","Create Table Successfully - classs");
}
// Insert Data
public long InsertData(String strServiceName) {
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
ContentValues Val = new ContentValues();
Val.put("ServiceName", strServiceName);
long rows = db.insert(TABLE_MEMBER, null, Val);
db.close();
return rows; // return rows inserted.
} catch (Exception e) {
return -1;
}
}
public boolean Exists(String strServiceName) {
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.rawQuery("select 1 from "+TABLE_MEMBER+ "where ServiceName= ?",
new String[] { strServiceName });
boolean exists = (cursor.getCount() > 0);
cursor.close();
return exists;
}
Service:
public class Service implements Serializable
{
private static final long serialVersionUID = 1L;
private String name;
public Service() {
// TODO Auto-generated constructor stub
}
public Service(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Aucun commentaire:
Enregistrer un commentaire