Список рассылки Android с флажками не работает должным образом
Это касается вопроса утилизации. Я использую пользовательский адаптер для заполнения списка. В пользовательской строке есть вид изображения, два текстовых поля и флажок. Все элементы заполняются, но флажок заполнен неправильно.
ВнутриgetView()
Я выполняю условие, и если условие выполняется, я устанавливаю флажок для включения состояния. Это работает хорошо, но с правильным флажком, который отмечен, есть и другие флажки, отмеченные флажком. Я прошел через много подобных вопросов переполнения стека, но не смог найти ответ. Любая помощь очень ценится.
Ниже мой класс адаптера:
public class LocationsListAdapter extends BaseAdapter {
List data;
Context context;
Locations userSelectedLocation;
private SharedPreferences locationPreferences;
private SharedPreferences.Editor locationPrefsEditor;
public LocationsListAdapter(List data, Context c) {
this.data = data;
this.context = c;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressWarnings("static-access")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.find_your_location_row, null);
holder = new ViewHolder();
holder.LocationImage = (SmartImageView) convertView.findViewById(R.id.loca_row_image);
holder.locationName = (TextView) convertView.findViewById(R.id.txt_loca_name);
holder.LocationDescription = (TextView) convertView.findViewById(R.id.txt_loca_desc);
holder.locationCheckText = (TextView) convertView.findViewById(R.id.txt_check);
holder.locationCheck = (CheckBox) convertView.findViewById(R.id.location_check);
holder.locationCheck.setOnCheckedChangeListener(myCheckChangList);
convertView.setTag(holder);
locationPreferences = context.getSharedPreferences("locationPrefs", context.MODE_PRIVATE);
locationPrefsEditor = locationPreferences.edit();
String locationID = locationPreferences.getString("locationID", "");
try {
if(locationID.contains(String.valueOf(data.get(position).getLocationID()))){
holder.locationCheck.setChecked(true);
}
} catch (Exception e) {
Log.e("Fatal", " Exception");
}
holder.locationCheck.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
userSelectedLocation = data.get(position);
locationPreferences = context.getSharedPreferences("locationPrefs", context.MODE_PRIVATE);
locationPrefsEditor = locationPreferences.edit();
String userSelectedLocationID = userSelectedLocation.getLocationID();
locationPrefsEditor.clear();
locationPrefsEditor.putString("locationID", userSelectedLocationID);
locationPrefsEditor.commit();
Intent intent = new Intent(context, HomeScreen.class);
context.startActivity(intent);
Log.e("Check Box ", "Clicked");
}
});
}else {
holder = (ViewHolder) convertView.getTag();
}
final Locations location = data.get(position);
holder.LocationImage.setImageUrl(location.getImagePath());
holder.locationName.setText(location.getLocationName());
holder.LocationDescription.setText(location.getLocationDescription());
return convertView;
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (observer != null) {
super.unregisterDataSetObserver(observer);
}
}
protected class ViewHolder {
protected SmartImageView LocationImage;
protected TextView locationName;
protected TextView LocationDescription;
protected TextView locationCheckText;
protected CheckBox locationCheck ;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.e("checked", "");
}
};
}