當這個問題被谷歌搜索時,我已經花了至少幾個小時在網上查看所有的代碼,但仍然不知道如何實現這一點。(android studio)使用複選框刪除列表視圖項目
我有一個由遊標適配器填充數據庫的列表視圖。 每個listview項目旁邊都有一個複選框。
我想從列表視圖中刪除所有已選中複選框的列表視圖項目。
我需要什麼代碼在我的刪除按鈕來實現呢?
感謝和親切的問候。
之前有人提到這個問題之前已經問過;每個問題都有自己的實現和代碼在問題中,我一直無法找到一步一步的教程。
當這個問題被谷歌搜索時,我已經花了至少幾個小時在網上查看所有的代碼,但仍然不知道如何實現這一點。(android studio)使用複選框刪除列表視圖項目
我有一個由遊標適配器填充數據庫的列表視圖。 每個listview項目旁邊都有一個複選框。
我想從列表視圖中刪除所有已選中複選框的列表視圖項目。
我需要什麼代碼在我的刪除按鈕來實現呢?
感謝和親切的問候。
之前有人提到這個問題之前已經問過;每個問題都有自己的實現和代碼在問題中,我一直無法找到一步一步的教程。
我的解決方案:
這裏是我的適配器:
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);
}
我如何使用按鈕而不是工具欄中的圖標進行此項工作? – kdasdaskl
在活動中放置一個按鈕 – XxGoliathusxX
'每個問題都有自己的問題實現和代碼。「所以請提供你所做的代碼,我的心靈能力inst還可用。 – Enzokie