안드로이드 리스트뷰의 체크박스를 선택해 지우고 싶습니다. (DB연동된 리스트뷰)

조회수 1242회

코드가 길어서죄송합니다 ㅠㅠ 현재 구현한게 MainActivity에서 FrameLayout을 통해 버튼으로 3개의 Fragment를 따로따로 동작시키고 있습니다. 그 중 listview를 띄우는 Fragment에서 listView를 삭제하는 부분에서 막혀서 질문합니다. ListView에는 2개의 TextView와 1개의 Checkbox가 Custom으로 지정되어 출력됩니다. 이미지 제가 구현하고자 하는건 Fragment에서 삭제 버튼을 눌렀을때 체크박스가 체크된 리스트뷰들이 DB에서 삭제되게 하고 싶은데.. 어떻게 해야할지 감이 잘 안잡힙니다. 어떻게 구현해야할지 알려주시면 정말 감사하겠습니다 :)

Adapter 의 getView메소드 내용입니다.

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if(convertView == null){
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(resId, null);
        }
        // findViewById를 대신한 ViewHolder객체
        holder = new ViewHolder();
        holder.engstr = (TextView) convertView.findViewById(R.id.engText);
        holder.hanstr = (TextView) convertView.findViewById(R.id.hanText);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.del_check);
        convertView.setTag(holder);

        StrData data = datas.get(position);
        holder.engstr.setText(data.engstr);
        holder.hanstr.setText(data.hanstr);
        holder.checkBox.setChecked(false);
        holder.checkBox.setChecked(((ListView)parent).isItemChecked(position));

        return convertView;
    }
    private class ViewHolder{
        TextView engstr;
        TextView hanstr;
        CheckBox checkBox;
    }

DataBaseHelper 의 내용입니다.

public class DatabaseHelper extends SQLiteOpenHelper {
    public static String NAME = "Database.db";
    public static int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) { super(context, NAME, null, DATABASE_VERSION); }

    // DatabaseHelper 호출시 한번만 실행
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sentenceSql = "CREATE table tb_sentence (" +
                "_id integer primary key autoincrement," +
                "engstr not null," +
                "hanstr not null," +
                "checkbox)";
        db.execSQL(sentenceSql);
    }

    // 버전업그레이드(개발자 테스트 단계)시 테이블 삭제 및 DB 재생성
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(newVersion == DATABASE_VERSION){
            db.execSQL("drop table tb_sentence");
            onCreate(db);
        }
    }
}

ListView가 구현되어있는 프레그먼트의 일부소스입니다.

    public void onResume() {
        super.onResume();
        btn_event();

        DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT * from tb_sentence order by engstr", null); // engstr의 순서대로 배치
        datas = new ArrayList<>();
        // 커서를 이동하며 데이터베이스를 읽어옴
        while (cursor.moveToNext()) {
            StrData data = new StrData();
            data.engstr = cursor.getString(cursor.getColumnIndex("engstr"));
            data.hanstr = cursor.getString(cursor.getColumnIndex("hanstr"));
            datas.add(data);
        }
        db.close();

        // listview_item.xml 레이아웃을 통한 custom listview
        adapter = new ListAdapter(getContext(), R.layout.listview_item, datas);
        listView.setAdapter(adapter);
    }
  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)