我有一个Activity,它显示已保存的视图,包含来自数据库(存储,......)的数据 我想从ListView抛出onLongClick
中删除一个文件最新文件位于顶部
日志也是正确的(位置,......)
但每次最后图片都被移除
现在注意到:在OnItemClickListener上是同样的问题!
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(ShowClippingsActivity.this,
ShowClippingDetailsActivity.class);
intent.putExtra(ShowClippingsActivity.EXTRA_POSITION, position);
startActivity(intent);
overridePendingTransition(R.anim.in, R.anim.out);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> parent, View view,
final int position, long id) {
ShowClippingsActivity.this.removeClipping(position);
return true;
}
});
public void removeClipping(final int position) {
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(
ShowClippingsActivity.this);
// 2. Chain together various setter methods to set the dialog
// characteristics
builder.setMessage(R.string.dialog_message).setTitle(
R.string.dialog_title);
builder.setPositiveButton(R.string.dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//TODO BUG - removes always last image
// User clicked OK button
// Get the _ID of the clipping to delete
ShowClippingsActivity.this.mCursor.moveToPosition(position);
int clippingId = ShowClippingsActivity.this.mCursor.getInt(ShowClippingsActivity.this.mCursor.getColumnIndex(Clipping._ID));
// Define 'where' part of query.
String selection = Clipping._ID + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { String.valueOf(clippingId) };
// Issue SQL statement.
int deletedRows = ShowClippingsActivity.this.mDb.delete(
Clipping.TABLE_NAME, selection,
selectionArgs);
Log.d(TAG, "--------------");
//Log.d(TAG, "positon: " + id);
Log.d(TAG, "id: " + String.valueOf(position));
Log.d(TAG, "sql: " + Clipping.TABLE_NAME + " | " + selection + " | " + selectionArgs[0]);
Log.d(TAG, "deletedRows: " + String.valueOf(deletedRows));
ShowClippingsActivity.this.mCursor = mDb.query(Clipping.TABLE_NAME, // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
ShowClippingsActivity.this.mListAdapter.swapCursor(ShowClippingsActivity.this.mCursor);
ShowClippingsActivity.this.mListAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton(R.string.dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
}
Clipping.java
/**
* This class represents a clipping.
*/
public class Clipping {
/**
* Unique clipping id
*/
private int clippingId;
/**
* Timestamp of the creation of the clipping
*/
private int time;
/**
* Textual describtion of the clipping
*/
private String text;
/**
* Title of the clipping
*/
private String title;
/**
* Link to the source of the clipping
*/
private String deeplink;
/**
* Flag to mark a clipping as bookmarked
*/
private int readLater;
/**
* Returns clipping id
* @return clipping id
*/
public int getClippingId() {
return clippingId;
}
/**
* Sets the clipping id
* @param clippingId clipping id
*/
public void setClippingId(int clippingId) {
this.clippingId = clippingId;
}
/**
* Returns the timestamp of creation of the clipping
* @return timestamp of creation of the clipping
*/
public int getTime() {
return time;
}
/**
* Sets the timestamp of creation of the clipping
* @param time timestamp of creation of the clipping
*/
public void setTime(int time) {
this.time = time;
}
/**
* Returns the textual description of the clipping
* @return textual description of the clipping
*/
public String getText() {
return text;
}
/**
* Sets the textual description of the clipping
* @param text textual description of the clipping
*/
public void setText(String text) {
this.text = text;
}
/**
* Returns the title of the clipping
* @return title of the clipping
*/
public String getTitle() {
return title;
}
/**
* Sets the title of the clipping
* @param title title of the clipping
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Returns the link to the source of the clipping
* @return link to the source of the clipping
*/
public String getDeeplink() {
return deeplink;
}
/**
* Sets the link of the source of the clipping
* @param deeplink link to the source of the clipping
*/
public void setDeeplink(String deeplink) {
this.deeplink = deeplink;
}
/**
* Returns the flag, which marks the clipping as a bookmark
* @return flag, which marks the clipping as a bookmark
*/
public int getReadLater() {
return readLater;
}
/**
* Sets the flag, which marks the clipping as a bookmark
* @param readLater flag, which marks the clipping as a bookmark
*/
public void setReadLater(int readLater) {
this.readLater = readLater;
}
}
答案 0 :(得分:0)
使用(我假设适配器中的对象是Clipping类型):
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> parent, View view,
final int position, long id) {
Clipping clipping = (Clipping)ShowClippingsActivity.this. mListAdapter.getItem(position);
ShowClippingsActivity.this.removeClipping(clipping.getId());
return true;
}
});
答案 1 :(得分:0)
从onItemLongClick获取的行id int是适配器中视图的索引。它类似于位置值,但会考虑不一定由列表中的项目支持的视图(例如标题视图或页脚视图)。通常,此索引将与相应数据库条目的id不匹配。当您调用removeClipping时,您需要传入与您要删除的数据库条目正确相关的ID。
@Override
public boolean onItemLongClick(final AdapterView<?> parent, View view, final int position, long id) {
// we have a view, but what we really need is the Clipping item
Clipping clipping = (Clipping) ShowClippingsActivity.this.mListAdapter.getItem(position);
// now we get the correct id that relates to the database
long clippingId = clipping.getDatabaseId();
// now we can prompt the user to delete the correct entry from the database
ShowClippingsActivity.this.removeClipping(clippingId);
// and lastly indicate that we handled the click event
return true;
}