我正在尝试将SQL中的ID从一个活动传递到OnItemClickListener中的RecyclerView中的另一个活动,但我没有收到接收活动中的数据。
这是我发送数据的活动(已编辑):
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public final static String KEY_EXTRA_BIRTHDAY_ID = "KEY_EXTRA_BIRTHDAY_ID";
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
GridAdapter mGridAdapter;
DBHelper dbh;
String firstName;
Animation shakeAnimation;
ImageView deleteImage;
List<Birthday> birthdays;
int birthdayId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
initAddImage();
dbh = new DBHelper(this);
initRecyclerView();
initDeleteImage();
}
public List<Birthday> getData() {
birthdays = new ArrayList<>();
Birthday birthday = null;
Cursor c = dbh.getBirthdayData();
if (c != null) {
while (c.moveToNext()) {
int nameIndex = c.getColumnIndex(dbh.BIRTHDAY_NAME);
String nameText = c.getString(nameIndex);
this.firstName = nameText;
int lastNameIndex = c.getColumnIndex(dbh.BIRTHDAY_LAST_NAME);
String lastNameText = c.getString(lastNameIndex);
int birthdayIdIndex = c.getColumnIndex(dbh.BIRTHDAY_ID);
birthdayId = c.getInt(birthdayIdIndex);
birthday = new Birthday();
birthday.setBIRTHDAY_ID(birthdayId);
birthday.setNAME(nameText);
birthday.setLAST_NAME(lastNameText);
birthdays.add(birthday);
}
}
return birthdays;
}
private void initRecyclerView(){
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setItemAnimator(new ScaleInAnimator());
// The number of Columns
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(mLayoutManager);
mGridAdapter = new GridAdapter(getData());
mRecyclerView.setAdapter(mGridAdapter);
mGridAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "It works!", Toast.LENGTH_SHORT).show();
// Cursor cursorItem = dbh.getBirthdayId(position);
// if( cursorItem != null && cursorItem.moveToFirst() ){
// birthdayId = cursorItem.getInt(cursorItem.getColumnIndex(DBHelper.BIRTHDAY_ID));
// cursorItem.close();
// }
Intent intent = new Intent(MainActivity.this, EditBirthdayActivity.class);
intent.putExtra(KEY_EXTRA_BIRTHDAY_ID, birthdayId);
startActivity(intent);
}
});
}
private void initAddImage(){
ImageView addImage = (ImageView) findViewById(R.id.add_image);
addImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddBirthday.class);
startActivity(intent);
}
});
}
private void initDeleteImage(){
deleteImage = (ImageView) findViewById(R.id.delete_image);
shakeAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake_animation);
deleteImage.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
initRecyclerView();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.delete_image:
mRecyclerView.startAnimation(shakeAnimation);
if (v.equals(deleteImage)) {
mGridAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
dbh.deleteBirthday(position);
mGridAdapter.removeItem(position);
mRecyclerView.clearAnimation();
}
});
}
}
}
} super.onResume();
initRecyclerView();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.delete_image:
mRecyclerView.startAnimation(shakeAnimation);
if (v.equals(deleteImage)) {
mGridAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
dbh.deleteBirthday(position);
mGridAdapter.removeItem(position);
mRecyclerView.clearAnimation();
}
});
}
}
}
}
这是接收者活动:
public class EditBirthdayActivity extends AppCompatActivity implements View.OnClickListener {
private DBHelper dbh;
EditText inputFirstName;
EditText inputLastName;
Button saveButton;
Button editButton, deleteButton;
int birthdayId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
birthdayId = getIntent().getIntExtra(MainActivity.KEY_EXTRA_BIRTHDAY_ID, 0);
setContentView(R.layout.edit_birthday);
dbh = new DBHelper(this);
init();
if (birthdayId > 0) {
saveButton.setVisibility(View.GONE);
editButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
Cursor rs = dbh.getBirthdayId(birthdayId);
rs.moveToFirst();
String getName = rs.getString(rs.getColumnIndex(DBHelper.BIRTHDAY_NAME));
String getLastName = rs.getString(rs.getColumnIndex(DBHelper.BIRTHDAY_LAST_NAME));
if (!rs.isClosed()) {
rs.close();
}
inputFirstName.setText(getName);
inputFirstName.setFocusable(false);
inputFirstName.setClickable(false);
inputLastName.setText(getLastName);
inputLastName.setFocusable(false);
inputLastName.setClickable(false);
}
}
private void init(){
inputFirstName = (EditText) findViewById(R.id.input_first_name);
inputLastName = (EditText) findViewById(R.id.input_last_name);
saveButton = (Button) findViewById(R.id.save_button);
editButton = (Button) findViewById(R.id.edit_button);
deleteButton = (Button) findViewById(R.id.delete_button);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save_button:
saveBirthday();
return;
case R.id.edit_button:
saveButton.setVisibility(View.VISIBLE);
editButton.setVisibility(View.GONE);
deleteButton.setVisibility(View.GONE);
inputFirstName.setEnabled(true);
inputFirstName.setFocusableInTouchMode(true);
inputFirstName.setClickable(true);
inputLastName.setEnabled(true);
inputLastName.setFocusableInTouchMode(true);
inputLastName.setClickable(true);
return;
case R.id.delete_button:
AlertDialog.Builder builder = new AlertDialog.Builder(EditBirthdayActivity.this);
builder.setMessage(R.string.delete_message)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dbh.deleteBirthday(birthdayId);
Toast.makeText(EditBirthdayActivity.this, "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(EditBirthdayActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// User cancelled the dialog..
}
});
AlertDialog d = builder.create();
d.setTitle(R.string.delete_title);
d.show();
return;
}
}
public void saveBirthday(){
String getName, getLastName;
getName = inputFirstName.getText().toString();
getLastName = inputLastName.getText().toString();
if (birthdayId > 0) {
if (dbh.updateBirthday(birthdayId, inputFirstName.getText().toString(),
inputLastName.getText().toString())) {
Toast.makeText(EditBirthdayActivity.this, "Updated Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(EditBirthdayActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
Toast.makeText(EditBirthdayActivity.this, "Update Failed", Toast.LENGTH_SHORT).show();
}
} else {
if (dbh.insertBirthday(getName, getLastName)){
Toast.makeText(EditBirthdayActivity.this, "Birthday Inserted!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(EditBirthdayActivity.this, "Couldn't insert Birthday!", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(EditBirthdayActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}
编辑:(数据库类)
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "_database";
private static final String BIRTHDAY_TABLE_NAME = "birthday_table";
public static final String BIRTHDAY_ID = "birthday_id";
public static final String BIRTHDAY_NAME = "birthday_name";
public static final String BIRTHDAY_LAST_NAME = "birthday_last_name";
private static final String CREATE_TABLE = "CREATE TABLE " + BIRTHDAY_TABLE_NAME + " ( "
+ BIRTHDAY_ID + " INTEGER PRIMARY KEY,"
+ BIRTHDAY_NAME + " TEXT,"
+ BIRTHDAY_LAST_NAME + " TEXT );";
SQLiteDatabase database;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + BIRTHDAY_TABLE_NAME);
onCreate(db);
}
public void setBirthdayData(String birthdayName, String birthdayLastName) {
database = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(BIRTHDAY_NAME, birthdayName);
cv.put(BIRTHDAY_LAST_NAME, birthdayLastName);
database.insert(BIRTHDAY_TABLE_NAME, null, cv);
}
public boolean insertBirthday(String birthdayName, String birthdayLastName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(BIRTHDAY_NAME, birthdayName);
contentValues.put(BIRTHDAY_LAST_NAME, birthdayLastName);
db.insert(BIRTHDAY_TABLE_NAME, null, contentValues);
return true;
}
public Cursor getBirthdayId(int id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + BIRTHDAY_TABLE_NAME + " WHERE " +
BIRTHDAY_ID + "=?", new String[] { Integer.toString(id) } );
return res;
}
public Cursor getBirthdayData() {
database = getReadableDatabase();
String[] columns = {BIRTHDAY_ID, BIRTHDAY_NAME, BIRTHDAY_LAST_NAME};
Cursor c = database.query(BIRTHDAY_TABLE_NAME, columns, null, null, null, null, BIRTHDAY_ID + " DESC");
return c;
}
public Cursor getBirthdayName(String[] args) {
database = getReadableDatabase();
String query = "SELECT " + BIRTHDAY_NAME + " FROM " + BIRTHDAY_TABLE_NAME + " WHERE " + BIRTHDAY_NAME + " =?";
Cursor c = database.rawQuery(query, args);
return c;
}
public boolean updateBirthday(Integer id, String birthdayName, String birthdayLastName) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(BIRTHDAY_NAME, birthdayName);
cv.put(BIRTHDAY_LAST_NAME, birthdayLastName);
db.update(BIRTHDAY_TABLE_NAME, cv, BIRTHDAY_ID + " = ? ", new String[] {Integer.toString(id)} );
return true;
}
public boolean deleteItem(long birthdayId) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(BIRTHDAY_TABLE_NAME, BIRTHDAY_ID + "=" + birthdayId, null) > 0;
}
public Integer deleteBirthday(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(BIRTHDAY_TABLE_NAME,
BIRTHDAY_ID + " = ? ",
new String[] { Integer.toString(id) });
}
}
答案 0 :(得分:1)
您的数据库实施可能存在错误。我不明白你如何使用getBirthdayId
方法将GridView位置映射到数据库ID。您可能希望将DBHelper
的代码添加到帖子中。
快速解决方案可能是为您的Birthday
类添加ID属性,并在第一个活动的getData()
函数中设置该属性。然后,在onItemClick
回调中,您可以从适配器列表中获取您的ID,而不是访问可能对您的UI线程性能有害的数据库。
以下是您在onItemClick
中可以做的事情:
Birthday clickedBday = birthdays.get(position);
int birthdayId = clickedBday.getBIRTHDAY_ID()
Intent intent = new Intent(MainActivity.this, EditBirthdayActivity.class);
intent.putExtra(KEY_EXTRA_BIRTHDAY_ID, birthdayId);
startActivity(intent);