2016-07-24 170 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

您正在初始化您的bindView中的idSpeaker,它始终为您提供数据的最后一个id。所以当你删除你的表时,你的最后一个项目被删除。

你只需要您的一个行

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

它不工作。 cursor.getposition()=列表视图中的最后一个索引 据我所知,我需要在listview中查找索引行并将其转移到cursor.movetoposition(index); 然后将工作 –

+0

添加此 cursor.moveToPosition(位置)之前idSpeaker –