Android listview com caixas de seleção não se comportando como esperado
Isso é sobre a questão da reciclagem. Eu estou usando um adaptador personalizado para preencher a exibição de lista. Na linha personalizada, há uma visualização de imagem, duas caixas de texto e uma caixa de seleção. Todos os elementos são preenchidos, mas a caixa de seleção não é preenchida corretamente.
Dentro degetView()
Eu executo uma condição e se a condição for verdadeira, defino a caixa de seleção para ativar o estado. Isso funciona bem, mas com a caixa de seleção correta marcada, outras caixas de seleção também estão sendo marcadas. Eu passei por muitas perguntas semelhantes de estouro de pilha, mas não consegui encontrar uma resposta. Qualquer ajuda é muito apreciada.
Abaixo está minha classe de adaptador:
public class LocationsListAdapter extends BaseAdapter {
List<Locations> data;
Context context;
Locations userSelectedLocation;
private SharedPreferences locationPreferences;
private SharedPreferences.Editor locationPrefsEditor;
public LocationsListAdapter(List<Locations> 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", "");
}
};
}