- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 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/
类型new AdapterView.OnItemClickListener(){}必须实现继承的抽象方法AdapterView.OnItemClickListener.onItemClick(Adap
当我在以下位置输入此代码时,出现“OnItemClickListener 无法解析为类型”的错误: package com.funkystudios.android.facts; import and
我已经检查过解决方案 here , here , here和 here 。它们都不适合我。 我的问题如下:我有一个仅由复选框组成的 ListView。我想在单击复选框后立即将相关复选框的当前状态保存到
我在 Android Studio 中设置了一个 Listview,但需要帮助编写 OnItemClickListner。 我已经尝试过该代码,但似乎不起作用。 public class MainAc
啊,我在这里做错了什么我已经为 ListView 和 onClick 设置了 onItemClickListener将它带到不同的 Activity ,但它告诉我它必须使用 OnItemClickli
我有一个 fragment ,其中包含一个 recyclerview,它有一个交错的网格布局管理器,其中 View 持有者只有一个图像。我试图在选择菜单选项(删除图标)后为 recyclerView
DataAccess data = new DataAccess(db); List countries = data.getCountries(); setListAdapter(new Array
我正在寻求构建一个程序,让用户从文本列表中点击,这会将他们带到另一个 Activity 。 我知道 RecyclerView 没有点击监听器,就像 ListView 一样,但是经过几次尝试,我无法让它
我想要 OnitemClickListener 用于 listview 但它对我不起作用。它给 Logcat 错误。 您的内容必须有一个 ListView ,其 id 属性为 R.id.list 我的
我在 GridView 中有一个 ItemClickListener。但是我的 itemclicklistener 没有被调用。 gridview 的项目点击没有 Activity @Override
我有两个列表,一个是自定义适配器,另一个是数组适配器。所以我希望 onItemClickListener 与自定义适配器 ListView 一起使用,并且当我开始对数组适配器使用相同的 ListVie
我有一个药物列表,当用户点击其中一个药物时,它将转到下一个显示药物详细信息的 Activity .. 下面的代码是带有药物列表的 Activity ..我知道我必须使用onclicklistener.
我认为这个问题说明了一切:我想为 ListView 创建自定义 OnItemClickListener。我想添加和更改 OnItemClickListener 的参数,但我如何创建自己的参数以便在我单
我有一个列表 fragment 显示我的自定义列表项,它由包含图像和文本等的相对布局组成。
我在类中有两个 listview,我不想为每个 listview 实现 OnClickListener,而是希望类实现 OnClickListener 并在单个覆盖方法 OnClickListener
我想在此 ListView 中实现 OnItemClickListener ,但是当我为此添加代码时,即使没有错误,我的应用程序也无法运行。当我单击 Listview 项目时,它会自动关闭。请帮助我,
我正在申请。我想从网络上的 Json 文件中获取一些信息。这行得通。现在我想要一个 onItemClickListener 到带有内容的 ListView 的项目。 onItemClickListen
我在从 ListItems 中获取选定的字符串时遇到一个问题,例如:考虑我的 ListItem 有一些元素只是被认为是一个,两个,三个出现在我的 taskLit 中,现在如果我点击 ListItem
我为 AutoCompleteTextView 设置了 setOnItemClickListener,并在 onItemClick 中,我已经根据所选值实现了需要发生的情况。该程序通过查看所选项目的索
我是 Android 开发的新手,我正在尝试构建一个 ListView,它使用 gson 从 Web 服务获取数据。我有一个模型类、一个列表类、一个适配器类和一个 Activity 类。 该列表工作正
我是一名优秀的程序员,十分优秀!