У меня есть проект Todo в Android, использующий ListView.Каждый элемент моего списка просмотра имеет один переключатель для удаления задачи, один TextView и один ImageView.Проблема в том, что когда я проверяю переключатель конкретной задачи, задача удаляется (желаемое поведение), но она также проверяет следующую (без удаления).
Вот код моего адаптера:
public class Adapter extends BaseAdapter {
private List<Tache> lesTaches;
private Context context;
private LayoutInflater inflater;
public Adapter(Context context, List<Tache> list) {
this.context = context;
lesTaches = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return lesTaches.size();
}
@Override
public Object getItem(int position) {
return lesTaches.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
view = (View) inflater.inflate(R.layout.tache_item, parent, false);
} else {
view = (View) convertView;
}
TextView intitule = (TextView) view.findViewById(R.id.intitule);
intitule.setText(lesTaches.get(position).getIntitule());
final Switch switch1 = (Switch) view.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isPressed()) {
if(isChecked) {
remove(lesTaches.get(position));
}
}
}
});
return view;
}
public void remove(Tache toDel) {
lesTaches.remove(toDel);
notifyDataSetChanged();
}
public void addItem(Tache toAdd) {
lesTaches.add(toAdd);
notifyDataSetChanged();
}}
А вот мой tache_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginHorizontal="5dp"
android:focusable="false" />
<TextView
android:id="@+id/intitule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/switch1"
android:layout_toRightOf="@+id/switch1" />
<ImageView
android:id="@+id/icon_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:layout_marginHorizontal="5dp"
app:srcCompat="@drawable/ic_info_black" />
</RelativeLayout>
Вот экран моего приложения, показывающий проблему (я удалил первое задание, поэтому мы его не видим, а второепроверено): Изображение, показывающее проблему:
![Image showing the problem](https://i.stack.imgur.com/A2PWq.jpg)
Я уже ищу в сети и проверяю, чтобы заменить setOnCheckedChangeListener на onClickListener, но появляется та же проблема.