Я столкнулся с раздражением и нигде не мог найти ответ.Итак, вот я.
У меня есть ListFragment, в который я помещаю данные, извлеченные из базы данных.Я расширил класс SimpleCursorAdapter и адаптировал его в соответствии со своими потребностями.
Мои элементы ListView выглядят следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDetailsListAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Amount"
android:textSize="23dp" />
<TextView
android:id="@+id/txtDetailsListComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Comment"
android:textSize="15dp" />
</LinearLayout>
<CheckBox
android:id="@+id/chkDetailsDebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Скрытый флажок отображается, как только я вызываю startActionMode.Цель этого флажка состоит в том, чтобы иметь возможность выбрать несколько элементов списка и удалить их все сразу.
Таким образом, удаление работает хорошо, когда есть только несколько записей и не требуется прокрутка.Тем не менее, как только я смогу прокрутить и представления перерабатываются, выбранные элементы списка изменятся.
Я видел решения, но они используют ArrayAdapters вместо расширенного SimpleCursorAdapter.
Вот мойпользовательский SimpleCursorAdapter:
public class CustomDetailsCursorAdapter extends SimpleCursorAdapter {
private DetailsActivity activity;
@SuppressWarnings("deprecation")
public CustomDetailsCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, Activity activity) {
super(context, layout, c, from, to);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.details_custom_list_item, parent,
false);
bindView(v, context, cursor);
return v;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
view.setTag(cursor.getString(cursor.getColumnIndex(Persons.COLUMN_ID)));
TextView name = (TextView) view
.findViewById(R.id.txtDetailsListComment);
String comment = cursor.getString(cursor
.getColumnIndex(Debts.COLUMN_COMMENT));
if(comment.equals("")){
comment = "-";
}
name.setText(comment);
TextView debt = (TextView) view.findViewById(R.id.txtDetailsListAmount);
double amount = Double.parseDouble(cursor.getString(cursor
.getColumnIndex(Debts.COLUMN_AMOUNT)));
DecimalFormat df = new DecimalFormat("#.##");
debt.setText(activity.getCurrency()+df.format(amount));
boolean isMyDebt;
if (cursor.getInt((cursor.getColumnIndex(Debts.COLUMN_IS_MY_DEBT))) == 0) {
isMyDebt = false;
} else {
isMyDebt = true;
}
if (!isMyDebt) {
debt.setTextColor(activity.getResources().getColor(
R.color.LawnGreen));
} else {
debt.setTextColor(activity.getResources().getColor(
R.color.Red));
}
}
}