我有一个包含SwitchCompat按钮的列表视图,当我在列表视图中的索引1上启用按钮时,再次当我在位置2启用按钮时,它会禁用位置1处的按钮。请帮助我,我不明白发生了什么。
代码: -
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
this.mContext = context;
existingDataSet = new ArrayList<>();
newDataSet = new ArrayList<>();
gson = new Gson();
sharedPreference = new MySharedPreference(mContext);
//retrieve data from shared preference
String jsonScore = sharedPreference.getAppsArrayListData();
Type type = new TypeToken<ArrayList<WhiteListModel>>() {
}.getType();
existingDataSet = gson.fromJson(jsonScore, type);
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder listViewHolder;
if (convertView == null) {
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list_item, parent, false);
listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon);
listViewHolder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.toggleButton);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ViewHolder) convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
listViewHolder.switchCompat.setOnCheckedChangeListener(null);
boolean isCheck = false;
AllAppList model = listStorage.get(position);
if (existingDataSet != null) {
for (int i = 0; i < existingDataSet.size(); i++) {
if (model.getPackName().equalsIgnoreCase(existingDataSet.get(i).getPackName())) {
isCheck = true;
}
}
listViewHolder.switchCompat.setChecked(isCheck);
}
listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
if (isChecked) {
new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle).setTitle("Warning").setMessage("You want to whiteList this application?").setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
//Adding items in Dataset
AllAppList appList = listStorage.get(position);
whiteListModel = new WhiteListModel();
whiteListModel.setName(appList.getName());
whiteListModel.setPackName(appList.getPackName());
if (existingDataSet != null) {
existingDataSet.add(whiteListModel);
saveScoreListToSharedpreference(existingDataSet);
} else {
newDataSet.add(whiteListModel);
saveScoreListToSharedpreference(newDataSet);
}
notifyDataSetChanged();
listViewHolder.switchCompat.setChecked(isChecked);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notifyDataSetChanged();
listViewHolder.switchCompat.setChecked(isChecked);
}
}).show();
} else {
AllAppList model = listStorage.get(position);
if (existingDataSet != null) {
for (int i = 0; i < existingDataSet.size(); i++) {
if (model.getPackName().equalsIgnoreCase(existingDataSet.get(i).getPackName())) {
existingDataSet.remove(i);
}
}
saveScoreListToSharedpreference(existingDataSet);
notifyDataSetChanged();
listViewHolder.switchCompat.setChecked(false);
Toast.makeText(mContext, "Removed", Toast.LENGTH_LONG).show();
}
}
}
});
return convertView;
}
/**
* Save list of scores to own sharedpref
*
* @param whiteListApps
*/
private void saveScoreListToSharedpreference(ArrayList<WhiteListModel> whiteListApps) {
Gson gson = new Gson();
//convert ArrayList object to String by Gson
String jsonScore = gson.toJson(whiteListApps);
Log.e(TAG, "LIST::" + jsonScore);
//save to shared preference
sharedPreference.saveAppsArrayListData(jsonScore);
}
private class ViewHolder {
SwitchCompat switchCompat;
TextView textInListView;
ImageView imageInListView;
}
}
答案 0 :(得分:0)
boolean isCheck = false; 使用数组的这个东西,并在该特定位置进行选中和取消选中。
如下所示; -
boolean isChecked []; 器isChecked [位置] = TRUE; 强>