在recyclerview中从数据库中删除项目

时间:2016-05-27 12:38:21

标签: android sqlite android-recyclerview

我想添加一项功能,允许我从recyclerview / database中删除一行。

此功能已集成到我的recyclerview的每个项目,如下图所示:

enter image description here

MySQLite.java:

public class MySQLite extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "character";
private static final int DATABASE_VERSION = 1;
private static final String CHARACTER_TABLE = "Ichar";
private static final String CHAR_TABLE = "create table " + CHARACTER_TABLE + "(id INTEGER PRIMARY KEY AUTOINCREMENT, nom TEXT, prenom TEXT , numero TEXT)";


Context context;


public MySQLite(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CHAR_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    onCreate(db);
}


public void InsererBDD(String nom, String prenom, String numero) {

    Log.d("insert", "before insert");

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues entree = new ContentValues();
    entree.put("nom", nom);
    entree.put("prenom", prenom);
    entree.put("numero", numero);

    db.insert(CHARACTER_TABLE, null, entree);

    db.close();
    Toast.makeText(context, "insérer entrée", Toast.LENGTH_LONG);

    Log.i("insert", "after insert");
    db.close();
}


public List<Character> donneesBDD() {

    List<Character> modelList = new ArrayList<Character>();
    String query = "select * FROM " + CHARACTER_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        do {
            Character model = new Character();
            model.setCharacter_Id(cursor.getInt(0));
            model.setNom(cursor.getString(1));
            model.setPrenom(cursor.getString(2));
            model.setNumero(cursor.getString(3));

            modelList.add(model);

        } while (cursor.moveToNext());
    }

    Log.d("donnee character", modelList.toString());


    return modelList;

}

public void supprimerLigne(int character_Id){

    SQLiteDatabase db = getWritableDatabase();
    db.delete(CHARACTER_TABLE , "id" + " = ?", new String[] { String.valueOf(character_Id)});
    db.close();

}

public Character getCharacterById(int Id) {

    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT  " +
            "nom" + "," +
            "prenom" + "," +
            "numero" +
            " FROM  " + CHARACTER_TABLE
            + " WHERE  " +
            "id" + "=?";

    Character character = new Character();


    Cursor cursor = db.rawQuery(query, new String[]{String.valueOf(Id)});

    if (cursor.moveToFirst()) {
        do {
            character.character_Id = cursor.getInt(cursor.getColumnIndex("id"));
            character.nom = cursor.getString(cursor.getColumnIndex("nom"));
            character.prenom = cursor.getString(cursor.getColumnIndex("prenom"));
            character.numero = cursor.getString(cursor.getColumnIndex("numero"));

        } while (cursor.moveToNext());



    }
    cursor.close();
    db.close();
    return character;

}
}

MyAdapter.java:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

static List<Character> characters;
static Context context;

MyAdapter(Context context,List<Character> characters)
{

    this.characters = new ArrayList<Character>();
    this.context = context;
    this.characters = characters;

}

@Override

    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.list_cell, null);


    MyViewHolder myViewHolder = new MyViewHolder(itemLayoutView);
    return myViewHolder;
}

@Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
    holder.nom.setText(characters.get(position).getNom());
    holder.prenom.setText(characters.get(position).getPrenom());
}


@Override
public int getItemCount() {
    return characters.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener,View.OnClickListener, MenuItem.OnMenuItemClickListener {

    public TextView nom;
    public TextView prenom;
    public ImageButton delete;






    public MyViewHolder(final View itemLayoutView) {
        super(itemLayoutView);


        nom = ((TextView) itemLayoutView.findViewById(R.id.nom));
        prenom = ((TextView) itemLayoutView.findViewById(R.id.prenom));
        delete = (ImageButton) itemView.findViewById(R.id.delete);
        itemLayoutView.setOnClickListener(this);
        itemLayoutView.setOnCreateContextMenuListener(this);


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MySQLite sqlite = new MySQLite(context);
                sqlite.supprimerLigne(getAdapterPosition());

                }

        });

    }
        @Override
            public void onClick(View view) {

            Intent intent = new Intent(context, personne.class);
            Bundle extras = new Bundle();
            extras.putInt("position", getAdapterPosition());
            intent.putExtras(extras);
            context.startActivity(intent);

            Toast.makeText(MyAdapter.context, "Vous avez sélectionné un item" + getAdapterPosition(), Toast.LENGTH_LONG).show();

        }



    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {


        menu.setHeaderTitle("");
        menu.add(0, v.getId(), 0, "Modifier Contact");
        menu.add(0, v.getId(), 0, "Supprimer Contact");
    }

    @Override
    public boolean onMenuItemClick(MenuItem item)
    {



        return true;
    }
}

}
你可以指导我吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

您应该在onBindViewHolder方法中编写代码,如下所示。

               holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MySQLite sqlite = new MySQLite(context);
                    sqlite.supprimerLigne(characters.get(position).getId());
                    }
                });