Donc je sais que cette question a été postée quelques fois sur le site, mais mon cas est un peu différent.Cursor Adapter sélectionnant les mauvais éléments à supprimer?
Supposons que j'ai ces articles dans mon curseur (dans l'ordre) :
Alaska
Canada
United States of America
Mongolia
China
India
Lorsque je clique sur l'Alaska, le journal montre la Mongolie est cliqué, et juste après cela, si je choisis le Canada, il montre la Mongolie a été cliqué
puis après je clique sur la Mongolie, États-Unis d'Amérique a cliqué sur la place
Il est d'avoir ce comportement bizarre ci-dessus. Voici mon code pour mon adaptateur Cursor:
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;
}
});
Comment puis-je choisir l'élément droit de supprimer de l'adaptateur du curseur? Un peu d'aide serait apprécié ...