2016-07-24 2 views
1

나는 텍스트 상자와 이미지가있는 사용자 지정 목록을 만들었습니다. 이미지는 데이터베이스 sqlite에서 행을 삭제해야합니다.cursoradapter의 ListView에서 항목 삭제

내 조각 활동에 sqlitedatabase 쿼리 메서드를 사용하고 있습니다.

cursor = mSqLiteDatabase.rawQuery("SELECT liked_id as _id, liked_presentation_id FROM liked", null); 
LikedListCursorAdapter likedListCursorAdapter = new LikedListCursorAdapter(getActivity(), cursor); 
lvItems.setAdapter(likedListCursorAdapter); 

///////////// 내 cursoradapter

public class LikedListCursorAdapter extends CursorAdapter { 

SQLiteDatabase mSqLiteDatabase; 
int idSpeaker,idPresentation; 
TextView textViewName, textViewCompany, textViewTitle, textViewDate, textViewAboutReport; 
LinearLayout linearLayout; 

    public static ImageView imageViewStatus; 
    private DatabaseHelper mDatabaseHelper; 

public LikedListCursorAdapter(Context context, Cursor cursor) { 
    super(context, cursor); 

} 


@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return LayoutInflater.from(context).inflate(R.layout.liked_list, parent, false); 
} 

@Override 
public void bindView(View view, final Context context, final Cursor cursor) { 

    imageViewStatus = (ImageView) view.findViewById(R.id.imageViewStatus); 

    textViewTitle = (TextView) view.findViewById(R.id.textViewTitle); 
    textViewDate = (TextView) view.findViewById(R.id.textViewTime); 

    idSpeaker = cursor.getInt(cursor.getColumnIndex("_id")); 

///////////////delete item 

    imageViewStatus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.w("id",String.valueOf(idSpeaker)); 
      mDatabaseHelper = new DatabaseHelper(context); 

      mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); 

      mSqLiteDatabase.delete(mDatabaseHelper.TABLE_LIKED, 
        "liked_id = ?", 
        new String[] {String.valueOf(idSpeaker)}); 
      cursor.requery(); 


      notifyDataSetChanged(); 
     } 
    }); 

그러나, 항상 목록보기의 마지막 행을 제거합니다. (커서 위치가 마지막 임).

내가 클릭하면 listview에서 현재 위치를 얻는 방법을 모르겠습니다. 제발 도와주세요

답변

0

솔루션 : 나는 setOnClickListener 전에 커서의 위치를 ​​찾기위한 코드를 추가

.

final int position = cursor.getPosition(); 



imageViewStatus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int idSpeaker; 

       mDatabaseHelper = new DatabaseHelper(context); 
       mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); 


/////move to this position 

       cursor.moveToPosition(position); 
       idSpeaker = cursor.getInt(cursor.getColumnIndex("liked_id")); 

       Log.w("getPos",String.valueOf(cursor.getPosition())); 
       mSqLiteDatabase.delete(mDatabaseHelper.TABLE_LIKED, 
         "liked_id = ?", 
         new String[] {String.valueOf(idSpeaker)}); 
       cursor.requery(); 


       notifyDataSetChanged(); 
      } 
     }); 
0

idSpeaker은 항상 데이터의 마지막 ID를 제공하는 bindview에서 초기화합니다. 그래서 테이블에서 삭제할 때 마지막 항목이 삭제됩니다.

는 만에 바인드 뷰에서 한 줄

idSpeaker = cursor.getInt(cursor.getColumnIndex("_id"));

을 이동해야하는

imageViewStatus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      cursor.moveToPosition(position); 
      idSpeaker = cursor.getInt(cursor.getColumnIndex("_id")); 

      Log.w("id",String.valueOf(idSpeaker)); 
      mDatabaseHelper = new DatabaseHelper(context); 

      mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); 

      mSqLiteDatabase.delete(mDatabaseHelper.TABLE_LIKED, 
        "liked_id = ?", 
        new String[] {String.valueOf(idSpeaker)}); 
      cursor.requery(); 


      notifyDataSetChanged(); 
     } 
    }); 
+0

작동하지 않습니다. cursorview.getposition() = listview의 마지막 인덱스 필자가 아는 한, listview에서 인덱스 행을 찾아 cursor.movetoposition (index)로 전송해야합니다. 다음 작동합니다 –

+0

이 추가 cursor.moveToPosition (위치) 전에 idSpeaker –