从Viewholder和数据库中删除项目

时间:2016-05-29 09:33:38

标签: android sqlite android-recyclerview adapter

我正在尝试从RecyclerView

中删除数据库中的项目

此代码返回错误:我的变量" position"在编译时需要是最终的(即使我在最后更改变量,它也不起作用)。

我不确定是否朝着正确的方向前进。

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 Button delete;

    public MyViewHolder(final View itemLayoutView) {
        super(itemLayoutView);
        nom = ((TextView) itemLayoutView.findViewById(R.id.nom));
        prenom = ((TextView) itemLayoutView.findViewById(R.id.prenom));
        delete = (Button) itemView.findViewById(R.id.delete);
        itemLayoutView.setOnClickListener(this);
        itemLayoutView.setOnCreateContextMenuListener(this);}
        public void onBindViewHolder(MyAdapter.MyViewHolder holder,  int position){
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MySQLite sqlite = new MySQLite(context);
                sqlite.supprimerLigne(characters.get(position).getCharacter_Id());
            }
        });
    }
        @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();
        }

和我的DBHelper:

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;
}
}

1 个答案:

答案 0 :(得分:1)

像这样改变 - &gt;

public void onBindViewHolder(MyAdapter.MyViewHolder holder, final int position) {
    holder.delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MySQLite sqlite = new MySQLite(context);
            sqlite.supprimerLigne(characters.get(position).getCharacter_Id());
        }
    });
}

您正在从内部函数访问position。所以你需要让变量最终。它是一个java基础。