마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

cursoradapter을 사용하여 db에서 리스트뷰를뿌리고 롱클릭 삭제 리스트뷰에서안없어져요

0 추천

제가 이렇게해서 해당하는 데이터를 db에서삭제하는거까진했는데 그러면 바로 리스트뷰에서 지워져야하잖아요 근데 안지워지고있다가 앱을껏다가 켜야지만 리스트뷰에서 안보이는데 혹시 리스트뷰에서도 데이터가없어지면 바로 없어지도록 반응하게끔 만들고싶거든요 ! DBAdapter라고 extends cursoradapter을 해서 자바파일을따로생성했고 그안에 제가만든 커스텀리스트뷰행 findviewbyid해서 객체잡아온담에 cursor이용해서 데이터넣어줬어요!

mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(final AdapterView<?> parent, View view, final int position, long id) {
        Toast.makeText(MainActivity.this,"롱클릭 이벤트 발생 ! ",Toast.LENGTH_LONG).show();
        AlertDialog.Builder alertDlg = new AlertDialog.Builder(view.getContext());
        alertDlg.setTitle("정말로 삭제하시겠습니까?");
       // TextView id1 = (TextView) view.findViewById(R.id.id_row);

      // final int pos = position;

      // '예' 버튼이 클릭되면
        alertDlg.setPositiveButton("예", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick( DialogInterface dialog, int which )
            {
              //final long id=0;
                //int position = cursor.getPosition();
                //Cursor c = (Cursor)parent.getItemAtPosition(position);
                cursor.moveToPosition(position);
                int _id = cursor.getInt(cursor.getColumnIndex(dbmanager.COLUMN_ID));
              sqlitedb = dbmanager.getWritableDatabase();
                String query="DELETE FROM customers WHERE _id= " + _id;
                sqlitedb.execSQL(query);
                //sqlitedb.execSQL(sql + id);
                startManagingCursor(cursor);


                //sqlitedb.delete(ids);
                cursor = sqlitedb.rawQuery(sql, null);
                //dbAdapter.swapCursor(cursor);
                //mListView.setAdapter(dbAdapter);
                dbAdapter.notifyDataSetChanged();

                dialog.dismiss();  // AlertDialog를 닫는다.

          }
        });
        // '아니오' 버튼이 클릭되면
        alertDlg.setNegativeButton( "아니오", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick( DialogInterface dialog, int which ) {
                dialog.dismiss();  // AlertDialog를 닫는다.
            }
        });

        alertDlg.show();

        return true;
    }
});
public class DBAdapter extends CursorAdapter {

    public DBAdapter(Context context, Cursor c) {
        super(context, c);
    }

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

       final ImageView image = (ImageView) view.findViewById(R.id.image_view);
        final TextView name = (TextView) view.findViewById(R.id.textname);
        final TextView date = (TextView) view.findViewById(R.id.textdate);
        TextView id1 = (TextView) view.findViewById(R.id.id_row);

        image.setImageResource(R.drawable.cake);

        name.setText(cursor.getString(cursor.getColumnIndex("name")));
        date.setText(cursor.getString(cursor.getColumnIndex("date")));

        int position = cursor.getPosition();
        id1.setTag(position);
    }
뚜루루루둡 (1,130 포인트) 님이 2016년 5월 30일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...