繁体   English   中英

自定义BaseAdapter上的开关问题

[英]Issue with Switch on custom BaseAdapter

我有一个片段,其中包含我的应用程序所有配置文件的列表。 此列表使用自定义BaseAdapter。 这是此适配器的规格:

  • 用户一次只能启用一个配置文件。
  • 如果用户尝试禁用所有配置文件,则会向用户发出警告,最后启用的配置文件将重新设置为启用。
  • 如果用户尝试启用未配置的配置文件,则会警告用户,并且上次启用的配置文件将重新设置为启用。
  • 用户可以配置每个配置文件。

一切正常,找到了,除了这是我当前的实现中存在的一个问题,该问题在第一次创建列表时就存在:

  • 如果用户在首次创建列表时启用(用户检查“切换”)列表中的第一个配置文件,然后尝试启用另一个配置文件,则第一个配置文件仍会被选中(“切换”仍处于选中状态)。 还要注意,即使选中了第一个配置文件,实际启用的配置文件也将是以后检查的配置文件。
  • 如果选择了其他配置文件,则不会发生这种情况。 每当启用其他配置文件时,所有其他配置文件都会被禁用。
  • 但是,在活动暂停并恢复后,一切都按预期工作。 这意味着仍然可以检查第一个开关的问题将消失。

这是此问题的图形表示: 在此处输入图片说明

这是承载BaseAdapter(AdapterProfiles)的此片段的代码。 PreferenceData是一个类,处理与共享首选项有关的所有事情。

public class Profiles extends Fragment implements OnCustomClickListener {

private View view_profiles;
private ListView lv_profiles;

private PreferenceData dataPreferenceObj;

private AdapterProfiles adapter;
private ArrayList<String> profileList = new ArrayList<String>();
private final ArrayList<HashMap<String, String>> profilesList = new ArrayList<HashMap<String, String>>();

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // ...
    lv_profiles = (ListView) view_profiles.findViewById(R.id.lv_profiles);
    // ...
}

@Override
public void onResume() {
    createProfileList();
    // Registers broadcast receiver

    super.onResume();
}

private void createProfileList() {
    if (profilesList.isEmpty() == true) {
        profileList = new ProfileList().getProfileList();

        for (int i = 0; i < profileList.size(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(Keys.KEY_PROFILE.toString(), profileList.get(i));
            profilesList.add(map);
        }
    }

    adapter = new AdapterProfiles(getActivity(), profilesList, this);
    lv_profiles.setAdapter(adapter);
}

/*
 * This is a custom onclick listener that gets handle from of list from
 * LazyAdapterProfiles
 */
@Override
public void OnCustomClick(View view, int position) {
    switch (view.getId()) {
    case R.id.iv_profiles_settings:
        // Opens setting for settings of profile clicked on
        break;
    case R.id.sw_profiles:
        Switch sw_profiles = (Switch) view.findViewById(R.id.sw_profiles);

        // Stops user from enabling profile that is not configured.
        if (dataPreferenceObj.getTemporaryRingtoneUri().equals("") == true
                || dataPreferenceObj.getTemporaryLocation().equals("") == true) {
            sw_profiles.setChecked(false);

            showWarningDialog(
                    "Profile Disabled",
                    "Profile location and ringtone must be selected in order to enable this profile.");
        } else if (dataPreferenceObj.getTemporaryRingtoneUri().equals("") != true
                && dataPreferenceObj.getTemporaryLocation().equals("") != true) {
            if (sw_profiles.isChecked() == true) {
                unselectProfileState(position);
            } else if (sw_profiles.isChecked() == false) {
                // Stops user from disabling profile as atleast one profile
                // must be enabled
                showWarningDialog("Profile Cannot Disable",
                        "Atleast one profile must be enabled");

                sw_profiles.setChecked(true);
            }

            // Rest of the code that writes the preference and broadcast it
        }
        break;
    }
}

// Gets switch map of all profiles and uncheck profile other than one that
// is selected.
private void unselectProfileState(int position) {
    for (int i = 0; i < profileList.size(); i++) {
        if (position != i) {
            AdapterProfiles.getMapHolder().get(i).sw_profiles
                    .setChecked(false);
        }
    }
}

}

我有一个自定义BaseAdapter,其每一行都包含TextView,ImageView和Switch,如下所示: 在此处输入图片说明

这是我在列表中创建行的BaseAdapter的实现:

public class AdapterProfiles extends BaseAdapter {

private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

private String profile_name;
private String retrived_profile_name;

private ViewHolder holder;
private Activity activity;

private OnCustomClickListener callback;
private static SparseArray<ViewHolder> mapHolder = new SparseArray<ViewHolder>();

public AdapterProfiles(Activity activity,
        ArrayList<HashMap<String, String>> data,
        OnCustomClickListener callback) {

    this.activity = activity;
    this.data = data;
    this.callback = callback;

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public AdapterProfiles(Activity activity,
        ArrayList<HashMap<String, String>> data) {
    this.data = data;

    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.ui_profiles_row, null);

        holder = new ViewHolder();

        holder.tv_profiles_name = (TextView) convertView
                .findViewById(R.id.tv_profiles_name);
        holder.tv_profiles_location_name = (TextView) convertView
                .findViewById(R.id.tv_profiles_location_name);
        holder.iv_profiles_settings = (ImageView) convertView
                .findViewById(R.id.iv_profiles_settings);
        holder.sw_profiles = (Switch) convertView
                .findViewById(R.id.sw_profiles);

        holder.iv_profiles_settings
                .setOnClickListener(new CustomOnClickListener(callback,
                        position));
        holder.sw_profiles.setOnClickListener(new CustomOnClickListener(
                callback, position));

        convertView.setTag(holder);

        mapHolder.put(position, holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    getPrefs(position);
    setUIData();

    return convertView;
}

private void getPrefs(int position) {
    HashMap<String, String> word = new HashMap<String, String>();
    word = data.get(position);

    profile_name = word.get(Keys.KEY_PROFILE.toString());

    SharedPreferences settings = activity.getSharedPreferences(
            Keys.PREFS_APP.toString(), 0);
    // This allows us to retrive unique data based on the profile names
    String PROFILE_LOCATION = Keys.KEY_LOCATION.toString() + "_"
            + profile_name;
    retrived_profile_name = settings.getString(Keys.KEY_PROFILE.toString(),
            "");
}

// Sets UI data of item in the list
private void setUIData() {
    if (profile_name.equals(retrived_profile_name) == true) {
        holder.sw_profiles.setChecked(true);
    } else if (profile_name.equals(retrived_profile_name) == false) {
        holder.sw_profiles.setChecked(false);
    }
}

public static class ViewHolder {
    public ImageView iv_profiles_settings;
    public TextView tv_profiles_location_name;
    public TextView tv_profiles_name;
    public Switch sw_profiles;
}

public static SparseArray<ViewHolder> getMapHolder() {
    return mapHolder;
}

}

如果有人想知道它是什么,最后是CustomOnClickListener类。 此类的实现是将回调映射到列表中的特定组件。 此类最初是由某人创建的(我没有在书签中找到它来归功于作者),我使用他/她的样本来适合自己的实现。 这是该代码:

public class CustomOnClickListener implements OnClickListener {
private int position;
private OnCustomClickListener callback;

public CustomOnClickListener(OnCustomClickListener callback, int position) {
    this.position = position;
    this.callback = callback;
}

@Override
public void onClick(View v) {
    callback.OnCustomClick(v, position);
}

}

public interface OnCustomClickListener {
    public void OnCustomClick(View view, int position);
}

现在,当我知道notifyDataSetChanged()方法将刷新视图上的更改时,在这里实现我自己的方法以取消选择适配器中的所有其他Switch-es似乎很愚蠢。 现在,我似乎已经通过删除以下代码解决了此问题:

if (sw_profiles.isChecked() == true) {
                unselectProfileState(position);
            } 

private void unselectProfileState(int position) {
for (int i = 0; i < profileList.size(); i++) {
    if (position != i) {
        AdapterProfiles.getMapHolder().get(i).sw_profiles
                .setChecked(false);
    }
}

}

并在OnCustomClick案例R.id.sw_profiles的末尾添加以下内容。 如前所述,这是Android文档中对这种方法的快速描述: “通知附加的观察者基础数据已更改,任何反映数据集的View均应刷新。”

adapter.notifyDataSetChanged();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM