Reputation: 2176
I am working on a checkbox listview. What I have done so far is populated the list from local db, get all the checked item's databse id using the setOnCheckedChangeListener
and store it in a separate table in my local db. This is how my code looks:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
dbHelper = new DBHelper(con);
database = dbHelper.getWritableDatabase();
switch (getItemViewType(position)){
case DYNAMIC_OBJECTIVE:
convertView = layoutInflater.inflate(R.layout.objective_list_view, null);
break;
case STATIC_OBJECTIVE:
convertView = layoutInflater.inflate(R.layout.other_objective_list_view, null);
break;
}
switch (getItemViewType(position)){
case DYNAMIC_OBJECTIVE:
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.objectiveCheckBox);
checkBox.setText(((Objectives)list.get(position)).getObjectiveTitle());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedObjectivesMap.put(((Objectives)list.get(position)).getObjectiveID(), "YES");
}else{
selectedObjectivesMap.put(((Objectives)list.get(position)).getObjectiveID(), "NO");
}
}
});
break;
case STATIC_OBJECTIVE:
TextView checkBoxOther = (TextView) convertView.findViewById(R.id.objectiveOtherCheckBox);
final TextView objectiveOtherET = (TextView) convertView.findViewById(R.id.objectiveOtherEditText);
checkBoxOther.setText((String)list.get(position));
//ll = (LinearLayout)convertView.findViewById(R.id.linearLayoutOther);
//viewNew = layoutInflater.inflate(R.layout.other_objective_row, null);
ImageButton imageButton = (ImageButton) convertView.findViewById(R.id.addOtherObjectiveTextBox);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), "Clicked123", Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.otherobjectivedialog);
final EditText enterObjET = (EditText) dialog.findViewById(R.id.enterObjEditText);
Button closeEnterObjBtn = (Button) dialog.findViewById(R.id.closeEnterObjDialog);
Button objConfirmBtn = (Button) dialog.findViewById(R.id.enterObjConfirmBtn);
closeEnterObjBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
objConfirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String enteredObj = enterObjET.getText().toString();
if(enteredObj.equals("")){
Toast.makeText(v.getContext(), "Please write a objective", Toast.LENGTH_LONG).show();
}else {
objectiveOtherET.setText(objectiveOtherET.getText().toString()+ " \n" + enteredObj);
otherObjList.add(enteredObj);
dialog.dismiss();
}
}
});
dialog.show();
/* LinearLayout linearLayoutNew = new LinearLayout(con);
linearLayoutNew.setOrientation(LinearLayout.VERTICAL);
et = new EditText(con);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setHint("Set some objective");
linearLayoutNew.addView(et);
ll.addView(linearLayoutNew);*/
}
});
break;
}
}
return convertView;
}
and this is how I'm populating my list:
ArrayList<Object> objectiveList = new ArrayList<>();
Cursor crs = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_OBJECTIVE +"", null);
while(crs.moveToNext())
{
String title = crs.getString(crs.getColumnIndex("title"));
int objectiveID = crs.getInt(crs.getColumnIndex("id"));
objectiveList.add(new Objectives(objectiveID, title));
}
crs.close();
objectiveList.add("Others");
listView.setAdapter(new ObjectivesAdapter(getActivity(), objectiveList));
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
PROBLEM: This all works perfect but now I want to edit the same information. All I want is the existing checkbox id's (that were checked and store in my db) should be checked when I come back to this view. I have queried the data and I have the IDs but I don't know how to check the checkboxes based on their IDs.
Cursor crsCheckObj = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_PLAN_OBJECTIVE_MAP +" WHERE "
+ ItemsTable.COLUMN_PLAN_OBJECTIVE_MAP_PLANID +"='"+ Info.getInstance().getPlanVisitID() +"'", null);
if(crsCheckObj.getCount() > 0){
while (crsCheckObj.moveToNext()){
//Here IDs should match in order to check the checkboxes
}
}
crsCheckObj.close();
Upvotes: 2
Views: 743
Reputation: 532
For that you can simply use checkbox.setChecked(true)
if it do not work then try following:
checkbox.postDelayed(new Runnable() {
@Override
public void run() {
checkbox.setChecked(true);
}
},100);
Upvotes: 1
Reputation:
First setTag()
for your checkbox like this:
checkBox.setTag(((Objectives)list.get(position)).getObjectiveID());
then all you need to is something like this:
Cursor crsCheckObj = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_PLAN_OBJECTIVE_MAP +" WHERE "
+ ItemsTable.COLUMN_PLAN_OBJECTIVE_MAP_PLANID +"='"+ Info.getInstance().getPlanVisitID() +"'", null);
if(crsCheckObj.getCount() > 0){
while (crsCheckObj.moveToNext()){
if(((Integer) checkBox.getTag()) == crsCheckObj.getInt(crsCheckObj.getColumnIndex("objective_id"))){
checkBox.setChecked(true);
}
}
}
crsCheckObj.close();
Upvotes: 0