我遇到了烦恼,我无法在任何地方找到答案。我在这里。
我有一个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));
}
}
}