2017-09-11 4 views
0

私はこの質問がサイトに数回掲載されていることを知っていますが、私の場合は少し異なっています。カーソルアダプタは間違った項目を選択して削除しますか?

私はカーソル内のこれらの項目 (順番に) があるとします。私はカナダを選択した場合、私はアラスカをクリックすると

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

カーソルは、現在の行への位置ポインタを持つデータベースからの単なる結果セットです。リストをスクロールし、カーソルデータに基づいて新しい行が塗りつぶされると、この位置ポインタは自動的にカバーの下に移動され、bindViewメソッドのカーソル位置が保存されます。

ブロー

@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 
         } 
        }); 

} 
ようbindViewであなたのonClickListenerを変更

関連する問題