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 메소드에서 커서 위치를 저장합니다.

는 타격처럼의 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 
         } 
        }); 

} 
관련 문제