所以我知道这个问题已在网站上发布了几次,但我的情况有点不同。
假设我的光标中有这些项目 (为了) :
Alaska
Canada
United States of America
Mongolia
China
India
当我点击阿拉斯加时,点击日志显示蒙古, 在此之后,如果我选择加拿大,则显示蒙古被点击
然后在我点击蒙古后,点击了美利坚合众国
上面有这种奇怪的行为。 这是我的光标适配器的代码:
public class EventListCursorAdapter extends CursorAdapter {
private LayoutInflater cursorInflater;
Calculations calculations = new Calculations();
Gson gson = new Gson();
Context AppContext;
DatabaseHelper dbHelper;
Context mContext;
String[] whereClause;
Intent mainActivityIntent;
public EventListCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AppContext = context.getApplicationContext();
mContext = context;
notifyDataSetChanged();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return cursorInflater.inflate(R.layout.card_view, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView timeText = (TextView) view.findViewById(R.id.event_time);
TextView nameText = (TextView) view.findViewById(R.id.event_name);
TextView dateText = (TextView) view.findViewById(R.id.event_date);
TextView summaryText = (TextView) view.findViewById(R.id.event_summary);
TextView locationText = (TextView) view.findViewById(R.id.event_location);
TextView categoryText = (TextView) view.findViewById(R.id.event_category);
CardView card = (CardView) view.findViewById(R.id.CardViewItem);
final Cursor mCursor = cursor;
String date = calculations.UnixTimeConverter(
mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_UNIXTIME)
))[0];
String time = calculations.UnixTimeConverter(
mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_UNIXTIME))
)[1];
final LatLng location = gson.fromJson(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_LOCATION)),LatLng.class);
nameText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
Log.i("bindView cursor check", "name=" + mCursor.getString((mCursor.getColumnIndex(DatabaseHelper.COL_NAME))));
dateText.setText(date);
timeText.setText(time);
summaryText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_SUMMARY)));
locationText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_LOCATIONNAME)));
categoryText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_CATEGORY)));
locationText.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final CameraPosition camLocation = CameraPosition.builder().target(location).zoom(18).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camLocation));
}
});
summaryText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater mLayoutInflator;
mLayoutInflator = LayoutInflater.from(AppContext);
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
View mView = mLayoutInflator.inflate(R.layout.summarydialog,null);
TextView textView = mView.findViewById(R.id.mainText);
textView.setText(
mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_SUMMARY))
);
textView.setMovementMethod(new ScrollingMovementMethod());
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
card.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
whereClause = new String[] {String.valueOf(mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_LOCALID)))};
Log.v("Where clause:",whereClause[0]);
Log.i("Event onLongClick", "name=" + mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
LayoutInflater mLayoutInflator;
mLayoutInflator = LayoutInflater.from(AppContext);
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
View mView = mLayoutInflator.inflate(R.layout.canceldelete_editor,null);
final TextView cancelButton = (TextView) mView.findViewById(R.id.cancelAction);
final TextView deleteButton = (TextView) mView.findViewById(R.id.deleteEntryAction);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
dbHelper = new DatabaseHelper(mContext);
final SQLiteDatabase db = dbHelper.getWritableDatabase();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
Log.v("Where clause changed:",whereClause[0]);
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
Log.v("Item Lond Clicked",mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int tru = db.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper.COL_LOCALID + " = " + mCursor.getInt(mCursor.getColumnIndex(DatabaseHelper.COL_LOCALID)), null);
Log.v("Deleting Item:",mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME))+"");
if(tru == 1){
Log.v("Delete: ", "SuccessFull!" + mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
}else{
Log.v("Delete: ", "Failed!" + mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
}
DataSenderToServer dataSenderToServer = new DataSenderToServer();
dataSenderToServer.eraseEntry(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_GLOBALID)));
mainActivityIntent = new Intent(mContext,MainActivity.class);
mContext.startActivity(mainActivityIntent);
dialog.dismiss();
mCursor.requery();
}
});
return true;
}
});
如何从光标适配器中选择要删除的项目? 一些帮助将不胜感激......
答案 0 :(得分:0)
Cursor只是数据库中的结果集,其中位置指针指向当前行。当您滚动列表并根据光标数据填充新行时,此位置指针将自动移动到封面下。然后在bindView方法中保存光标位置。
在bindView中更改onClickListener,如打击
@Override
public void bindView(View view, Context context, Cursor cursor) {
final int position = cursor.getPosition();
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cursor.moveToPosition(position);
//Now Do Your delete operation
}
});
}