私の解決策:
public class TextCheckBoxAdapter extends ArrayAdapter {
private List<String> names;
private List<Boolean> checked;
public TextCheckBoxAdapter(Context context, List<String> names) {
super(context, R.layout.custom_row_text_checkbox, names);
this.names = names;
checked = new ArrayList<>();
for(String name : names) checked.add(false);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.custom_row_text_checkbox, parent, false);
TextView tv = (TextView) customView.findViewById(R.id.nameTv);
tv.setText(names.get(position));
final CheckBox checkBox = (CheckBox) customView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checked.set(position, checkBox.isChecked());
}
});
return customView;
}
public String getName(int position){
return names.get(position);
}
public List<Boolean> getCheckList(){
return checked;
}
/**Anzahl selektierter Checkboxen*/
public int getCountSelectedCheckBoxes(){
int toReturn = 0;
for(boolean b : checked) if(b) toReturn++;
return toReturn;
}
public void delete(int i){
names.remove(i);
checked.remove(i);
notifyDataSetChanged();
}
public boolean isEmpty(){
return names.isEmpty();
}
}
レイアウト:ここで
は私のアダプタです//ツールバーのアイコンから削除
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="@dimen/custom_margin_left_and_right"
android:layout_marginStart="@dimen/custom_margin_left_and_right"
android:layout_marginRight="@dimen/custom_margin_left_and_right"
android:layout_marginEnd="@dimen/custom_margin_left_and_right">
<TextView
android:id="@+id/nameTv"
android:layout_weight="0.9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/unbenannt"
android:textColor="@color/grey"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_weight="0.1"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home){
onBackPressed();
}else if(id == R.id.action_loeschen){
if(adapter.getCountSelectedCheckBoxes() > 0){
List<Boolean> checked = adapter.getCheckList();
for(int i = checked.size() - 1; i >= 0; i--){ //Rueckwaerts ausfuehren!
if(checked.get(i)){
String name = adapter.getName(i);
//Loesche aus der Liste
adapter.delete(i);
//TODO: Delete from DB or whatever
//Checken ob Liste leer
checkIsListEmpty();
}
}
}else{
MyToast.showShort(this, getString(R.string.hinweis_keine_selektion));
}
}
return super.onOptionsItemSelected(item);
}
'すべての質問には独自の実装とコードがあります。あなたが作ったコード、私の精神的能力がまだあります。 – Enzokie