gpt4 book ai didi

android - 当我尝试使用 onItemClickListener 将数据传递给另一个 Activity 时,没有从 sqlite 获取数据

转载 作者:行者123 更新时间:2023-11-30 01:46:51 24 4
gpt4 key购买 nike

我试图在 OnItemClickListener 的 RecyclerView 中将 ID 从 SQLite 从一个 Activity 传递到另一个 Activity ,但我没有在接收 Activity 中获取数据。

这是我发送数据的 Activity (已编辑):

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

这是接收者 Activity :

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

最佳答案

您的数据库实现可能存在错误。我不明白您如何使用 getBirthdayId 方法将 GridView 位置映射到数据库 ID。您可能希望将 DBHelper 的代码添加到您的帖子中。

一个快速的解决方案可能是向您的 Birthday 类添加一个 ID 属性,并在第一个 Activity 的 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);

关于android - 当我尝试使用 onItemClickListener 将数据传递给另一个 Activity 时,没有从 sqlite 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33601124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com