【发布时间】:2017-06-23 10:08:59
【问题描述】:
我有一个模板页面,在这个页面上我可以添加和删除模板,给它们命名和描述。
每个模板都是一个对象,由名称、描述、权力和启用组成。
- 单击模板会加载另一个带有选项的列表视图。
- 每个选项都是名为 Option 的对象的一个实例。
- 每个选项都有一个我可以切换的开关。
- 当我保存模板时,我会在数据库中更新关于这些开关是打开还是关闭的值。
所以直接解决问题。
我们可以越过第一个模板列表视图进入下一个,即选项列表视图。
此列表视图仅包含 2 个选项,电源和启用。以下是我创建这些选项的方法。
TEMPLATEPOWER("Turn on device", "Switching this will turn on and off the fitting"),
TEMPLATEENABLED("Enable Device", "Switching this will enable and disable the device");
- 简单来说就是一个枚举
- 然后我有一个控制选项来处理这个枚举。
控制文件:
public ArrayList<Option> populateTemplateActions() {
actions.add(Option.TEMPLATEPOWER);
actions.add(Option.TEMPLATEENABLED);
return actions;
}
现在用于设置列表视图。为此:
- 我创建了一个选项数组并调用上述方法将这些枚举对象添加到数组中
- 然后我将此数组传递给适配器。
- 我还传递了片段,因为我需要能够在片段中设置模板对象。
填充数组:
templateOptions = control.populateTemplateActions();
创建列表视图:
adapter = new TemplateOptionAdapter(
getActivity(), templateOptions, template, ViewTemplate.this);
templateOptionsView.setAdapter(adapter);
adapter.notifyDataSetChanged();
进入适配器后,我会设置文本视图和开关。然后,我使用交换机上的更改侦听器来设置电源和启用选项。
- 我首先检查选项行对象,看它是 a) Power 还是 b) Enable。
- 然后我将其设置为选中或未选中。
- 然后侦听器侦听更改,并在更改发生时相应地设置方法。
- 然后我将这个更改的对象传回包含列表视图的片段中,这样我就可以将值插入到数据库中。
为了彻底,我将在下面发布整个适配器代码:
public class TemplateOptionAdapter extends BaseAdapter {
private Context context;
private ArrayList<Option> templateList;
private String name;
private Template template;
private Boolean checked;
int power;
int enable;
Option option;
ViewTemplate viewTemplate;
public TemplateOptionAdapter(Context context, ArrayList<Option> list, Template template, ViewTemplate viewTemplate) {
this.context = context;
templateList = list;
this.template = template;
this.viewTemplate = viewTemplate;
}
@Override
public int getCount() {
return templateList.size();
}
@Override
public Object getItem(int position) {
return templateList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
option = templateList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.template_option_view_row, null);
}
TextView templateName = (TextView) convertView.findViewById(R.id.option_name);
TextView templateDesc = (TextView) convertView.findViewById(R.id.option_info);
SwitchCompat optionSwitch = convertView.findViewById(R.id.optionSwitch);
name = option.getOptionName();
String description = option.getOptionDesc();
templateName.setText(name);
templateDesc.setText(description);
power = template.getTemplatePower();
enable = template.getTemplateStatus();
// Sets switches to their database values
switch (option) {
case TEMPLATEPOWER:
if (power == 0) {
optionSwitch.setChecked(false);
}
else {
optionSwitch.setChecked(true);
}
break;
case TEMPLATEENABLED:
if (enable == 0) {
optionSwitch.setChecked(false);
}
else {
optionSwitch.setChecked(true);
}
break;
}
// Monitors changing of switches and sets objects to be saved.
optionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checked = true;
switch (option) {
case TEMPLATEPOWER:
template.setTemplatePower(1);
Log.i("power", "power setting to 1" + "");
Log.i("enable", "enable dosnt change" + "");
break;
case TEMPLATEENABLED:
template.setTemplateStatus(1);
Log.i("power", "power dosnt change" + "");
Log.i("enable", "enable setting to 1" + "");
break;
}
} else {
checked = false;
switch (option) {
case TEMPLATEPOWER:
template.setTemplatePower(0);
Log.i("power", "power setting to 1" + "");
Log.i("enable", "enable dosnt change" + "");
break;
case TEMPLATEENABLED:
template.setTemplateStatus(0);
Log.i("power", "power dosnt change" + "");
Log.i("enable", "enable setting to 0" + "");
break;
}
}
}
});
viewTemplate.setTemplate(template);
return convertView;
}
哇,如果你还和我在一起很好,谢谢!
好的,手头的问题。
当我回到包含列表视图的片段时,当我单击一个按钮进行保存时: 无论我切换、电源还是启用,启用选项都是唯一改变的值。例如:
- 如果我将电源设置为打开,则将启用设置为打开。
- 如果我将电源设置为关闭,则启用会改为关闭。
所以在我的 switch 语句中,似乎从未见过 TEMPLATEPOWER 的情况,即使它是选项之一。
我是一名 android 新手开发者,我确信这是我的逻辑缺陷,我就是看不到哪里!
任何建议都会很棒,谢谢!
如果您需要更多说明,请询问:)
【问题讨论】:
-
我刚刚找到的另一个注释。如果我切换这两个选项,问题仍然存在,但问题是另一种方式。它只是选择列表中的最后一个选项....
标签: android listview debugging object switch-statement