android如何在 recyclerview 中获取的项 ID

标签: sqlite Android
发布时间: 2017/3/19 18:07:01
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我用幻灯片来从 recyclerview 中删除,但这个问题的时候,getAdapterPosition() 和 row_id 的 sql 表不相配。怎样才能从 id adapter 以及如何添加数据时,更新 recyclerview (它不显示新的数据,直到我重新打开应用程序或更改方向)?

MainActivity AppCompatActivity {类

RecyclerView mRecyclerView;
MyAdapter myAdapter;
ImageButton floatButton;

DbHelper dbHelper;

ArrayList<Information> data;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    floatButton = (ImageButton) findViewById(R.id.imageButton);
    floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, AddDetailPage.class);
            MainActivity.this.startActivity(intent);
        }
    });


    dbHelper = new DbHelper(this);

    data = new ArrayList<>();

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    myAdapter = new MyAdapter(this, showData());


    mRecyclerView.setAdapter(myAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


    ItemTouchHelper helper = new ItemTouchHelper(createHelperCallback());
    helper.attachToRecyclerView(mRecyclerView);
}

private ItemTouchHelper.Callback createHelperCallback() {
    ItemTouchHelper.SimpleCallback simpleItemCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT |
            ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            deleteItem(viewHolder.getAdapterPosition());
        }
    };
    return simpleItemCallback;
}


public ArrayList<Information> showData() {
    Cursor res = dbHelper.showData();
    while (res.moveToNext()) {
        Information information = new Information();
        information.ID = res.getString(0);
        information.first = res.getString(0);
        information.second = res.getString(2);
        information.detail = res.getString(3);
        data.add(information);
    }
    return data;
}

public void deleteItem(int position){
    DbHelper dbHelper = new DbHelper(this);
    dbHelper.delete(position);


    myAdapter.notifyItemRemoved(position);

    myAdapter.delete(position);
}

}

MyAdapter RecyclerView.Adapter {类

private MainActivity mainactivity;
private ArrayList<Information> data;
private LayoutInflater layoutInflater;

public MyAdapter(MainActivity mainActivity, ArrayList<Information> information) {
    this.mainactivity = mainActivity;
    this.data = information;
    layoutInflater = LayoutInflater.from(mainActivity);
    notifyDataSetChanged();
}


@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.list_item, parent, false);
    return new ViewHolder(view, data);
}


@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
    holder.first.setText(data.get(position).first);
    holder.second.setText(data.get(position).second);
    holder.detail.setText(data.get(position).detail);

}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView first, second, detail;
    ArrayList<Information> info;


    public ViewHolder(View itemView, ArrayList<Information> data) {
        super(itemView);

        this.info = data;

        first = (TextView) itemView.findViewById(R.id.firstText);
        second = (TextView) itemView.findViewById(R.id.secondText);
        detail = (TextView) itemView.findViewById(R.id.detail);
    }

}

public void delete(int x){
    data.remove(x);
    notifyDataSetChanged();
}

}

公共类 DbHelper 延伸 SQLiteOpenHelper {

public static final String DATABASE_NAME = "students.db";
public static final String TABLE_NAME = "student_table";
public static final String ID = "_id";
public static final String FIRST_NAME = "first_name";
public static final String SECOND_NAME = "second_name";
public static final String DETAIL = "detail";

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + FIRST_NAME + " TEXT ," + SECOND_NAME + " TEXT ," + DETAIL + " TEXT );");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String first, String second, String detail) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(FIRST_NAME, first);
    contentValues.put(SECOND_NAME, second);
    contentValues.put(DETAIL, detail);
    long x = db.insert(TABLE_NAME, null, contentValues);

    if (x == -1) {
        return false;
    } else {
        return true;
    }


}

public Cursor showData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return res;
}

public void delete(int x) {
    int y = x + 1;
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, ID + "=" + y, null);
}

}

解决方法 1:

创建这种方法在你 adapter 于 get id 的特定对象,通过 id 删除 DbHelper 类的功能。

int getId(int position){
    return data.get(position).ID;
}

和避免直接访问类的成员。而不是你可以使用 getter 和 setter 方法。

更新 recyclerview 添加新数据后,您可以在各自的插入方法在调用 notifyDataSetChanged() 你 adapter

编辑︰

要添加一个新的 element 到你的回收商 view 添加这种方法在你adapter

void addItem(Informationn infoObj){
     data.add(infoObj);
     notifyDataSetChanged();
}

调用此方法使用您 adapter 对象并传递参数以及你的 recyclerview 的新项目将更新与新项目

官方微信
官方QQ群
31647020