我正在制作一个复选框列表视图。到目前为止,我已经完成了从本地数据库填充列表,使用setOnCheckedChangeListener
获取所有已检查项目的数据库ID并将其存储在本地数据库中的单独表中。这就是我的代码的样子:
@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;
}
这就是我填充列表的方式:
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);
问题: 这一切都很完美,但现在我想编辑相同的信息。我想要的是当我回到这个视图时,应该检查现有的复选框ID(已经检查并存储在我的数据库中)。我查询了数据并且我有ID,但我不知道如何根据他们的ID检查复选框。
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();
答案 0 :(得分:1)
为此,您只需使用checkbox.setChecked(true)
如果不起作用,请尝试以下操作:
checkbox.postDelayed(new Runnable() {
@Override
public void run() {
checkbox.setChecked(true);
}
},100);
答案 1 :(得分:0)
您的复选框的第一个setTag()
如下:
checkBox.setTag(((Objectives)list.get(position)).getObjectiveID());
然后你需要的就是这样:
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();