我正在创建一个由数据库动态填充的交换机ListView。我需要验证是否至少选择了其中一个,并启用/禁用继续按钮。我将开关的值存储在HashMap中,以便我可以轻松检查它是否包含真值。
开关是在CursorAdapter中创建的,我尝试过设置OnClickListener和OnCheckedChangeListener。两者似乎都是,当单击一个项目时,监听器将触发列表中的每个按钮,然后我在HashMap中存储的值将被列表中下方按钮的已检查状态覆盖。
class LanguageInstallAdapter extends CursorAdapter {
private final String LOG_TAG = LanguageInstallAdapter.class.getSimpleName();
public HashMap<String,Boolean> mDBValues = new HashMap<String,Boolean>(); //original DB values
public HashMap<String,Boolean> mChangedValues = new HashMap<String,Boolean>();//anything changed
private HashMap<String,Boolean> mAllCurrentValues= new HashMap<String,Boolean>(); //complete current
private View mRootView;
public static class ViewHolder {
public final TextView nameView;
public final SwitchCompat installSwitch;
public ViewHolder(View view) {
nameView = (TextView) view.findViewById(R.id.language_textview);
installSwitch = (SwitchCompat) view.findViewById(R.id.installed_switch);
}
}
public LanguageInstallAdapter(Context context, Cursor c, int flags, View root) {
super(context, c, flags);
mRootView = root;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item_install_language, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
return view;
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
String languageId = cursor.getString(LanguageInstallFragment.COL_ID);
String name = cursor.getString(LanguageInstallFragment.COL_NAME);
boolean langInstalled = cursor.getInt(LanguageInstallFragment.COL_INSTALLED)>0; //convert DB 1 or 0 to java boolean
mDBValues.put(languageId, langInstalled);
mAllCurrentValues.put(languageId, langInstalled);
viewHolder.nameView.setText(name);
viewHolder.installSwitch.setChecked(langInstalled);
viewHolder.installSwitch.setTag(languageId);
viewHolder.installSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CompoundButton button = (CompoundButton) v;
String buttonLang= (String) button.getTag();
mChangedValues.put(buttonLang, button.isChecked());
mAllCurrentValues.put(buttonLang, button.isChecked());
setSubmitButton();
}
});
}
public void setSubmitButton(){
for (Map.Entry<String,Boolean> entry : mAllCurrentValues.entrySet()) {
Log.d(LOG_TAG, "Hashmap: " + entry.getKey() + " " + entry.getValue());
}
if(mAllCurrentValues.containsValue(true)) {
mRootView.findViewById(R.id.submit_button).setEnabled(true);
mRootView.findViewById(R.id.lang_prompt).setVisibility(View.GONE);
} else {
mRootView.findViewById(R.id.submit_button).setEnabled(false);
mRootView.findViewById(R.id.lang_prompt).setVisibility(View.VISIBLE);
}
}
}
从一系列选择中记录输出:
Initial state (none selected)
09-07 20:21:29.445 ﹕ Hashmap: 4 false
09-07 20:21:29.445 ﹕ Hashmap: 2 false
09-07 20:21:29.445 ﹕ Hashmap: 3 false
Select 2 (1 selected)
09-07 20:22:42.096 ﹕ Hashmap: 4 false
09-07 20:22:42.096 ﹕ Hashmap: 2 true
09-07 20:22:42.096 ﹕ Hashmap: 3 false
Select 3 (2 selected)
09-07 20:23:28.192 ﹕ Hashmap: 4 false
09-07 20:23:28.192 ﹕ Hashmap: 2 false
09-07 20:23:28.192 ﹕ Hashmap: 3 true
Select 4 (3 selected)
09-07 20:23:43.608 ﹕ Hashmap: 4 true
09-07 20:23:43.608 ﹕ Hashmap: 2 false
09-07 20:23:43.608 ﹕ Hashmap: 3 true
Unselect 2 (2 selected)
09-07 20:23:58.507 ﹕ Hashmap: 4 true
09-07 20:23:58.507 ﹕ Hashmap: 2 false
09-07 20:23:58.507 ﹕ Hashmap: 3 false
Unselect 3 (1 selected)
09-07 20:24:13.942 ﹕ Hashmap: 4 false
09-07 20:24:13.942 ﹕ Hashmap: 2 false
09-07 20:24:13.942 ﹕ Hashmap: 3 false
有没有办法不在每个按钮上调用监听器?即使调用每个按钮,为什么值被错误地覆盖?对于每个按钮的调用是否仍然不能获取该按钮的languageId和isChecked值,而不是覆盖另一个按钮?有没有更好的方法来做到这一点?
答案 0 :(得分:1)
替换此行
window.onload
与
mAllCurrentValues.put(languageId, langInstalled);