我正在尝试在我的应用中整理收藏夹。我有一个包含三列的数据库:
_id | name | description | star
1 London some desc.
2 Berlin some desc.
3 Paris some desc. yes
我想在listview中显示收藏夹。 refreshCursor()返回“star”列值为“yes”的名称列表:
private static final String COLUMN_STAR = "star";
private void refreshCursor() {
stopManagingCursor(cursor);
Intent intent = getIntent();
String TABLE_NAME = intent.getStringExtra("tableName");
cursor = database.query(TABLE_NAME, null, COLUMN_STAR + " = ?",
new String[] { "yes" }, null, null, null);
startManagingCursor(cursor);
}
没关系。
然后在我点击Paris
后,我发送了点击位置的额外字符串:
lvData.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String entryID = new Integer(position).toString();
Intent intent = new Intent();
intent.setClass(FavouritesActivity.this, ViewFavsActivity.class);
Bundle b = new Bundle();
b.putString("elementId", entryID);
intent.putExtras(b);
startActivity(intent);
}
});
但我得到(endtryID
)0
的位置,ViewFavsActivity
显示London
的解释。
如何获得Cursor
的实际位置并将其发送到ViewFavsActivity
?
求助。
部分FavouritesActivity
(onCreate)://方法refreshCursor位于
refreshCursor();
String[] from = new String[] { COLUMN_NAME };
int[] to = new int[] { R.id.tvText };
scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,
from, to);
lvData = (ListView) findViewById(R.id.list);
lvData.setAdapter(scAdapter);
lvData.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String entryID = String.valueOf(id);
Intent intent = new Intent();
intent.setClass(FavouritesActivity.this, ViewFavsActivity.class);
Bundle b = new Bundle();
b.putString("elementId", entryID);
intent.putExtras(b);
startActivity(intent);
ViewFavsActivity
的一部分:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// opening DB
refreshCursor();
bundle = getIntent().getExtras();
TextView titleText = (TextView) findViewById(R.id.titleText);
setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
null, null, null, null, null);
int entryID = Integer.parseInt(bundle.getString("elementId"));
setTitle.moveToPosition(entryID);
titleText.setText(setTitle.getString(setTitle
.getColumnIndex(COLUMN_DESC)));
}
private void refreshCursor() {
stopManagingCursor(cursor);
Intent intent = getIntent();
cursor = database.query(TABLE_NAME, new String[] { COLUMN_ID, COLUMN_STAR, COLUMN_DESC },
"_id = ? AND star = ?",
new String[] { intent.getStringExtra("elementId"), "yes" },
null, null, null);
}
加了:
关于pastebin的 关于pastebin的答案 0 :(得分:2)
真正的问题是你正在使用两个不同的Cursor。你从position == 0
得到:
cursor = database.query(TABLE_NAME, null, COLUMN_STAR + " = ?",
new String[] { "yes" }, null, null, null);
然后在:{/ p>中询问职位0
setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
null, null, null, null, null);
所以你获得不同结果的可能性很大。
此外 Bundle可以保存任何原始数据类型和一些更复杂的数据类型。因此,您不需要将数字转换为String,然后返回其原始数据类型......
更安全方法是使用行的ID,因此请在OnItemClickListener
中使用
b.putLong("elementId", id);
在ViewFavsActivity
:
long entryID = bundle.getLong("elementId");
setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
"_id = " + entryID, null, null, null, null);
(注意:如果有疑问总是使用?
和query()
的{{1}}参数来防止SQL注入攻击,但您正在使用{您作为索引检索的{1}},因此您在此上下文中是安全的。)
答案 1 :(得分:0)
这里的问题是你的Cursor只能产生一个对象。我相信是londe,是不是正确?如是。始终在列表视图中的位置结果中添加一个元素。例如:
如果列表视图只有一个对象:
lvData.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here will be showed the 0 position, not 3 like you whant.
String entryID = new Integer(position).toString();
}
});
解决方案:
您需要将方法中的视图转换为放置在适配器中的Object。例如:
lvData.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here will be showed the 0 position, not 3 like you whant.
MyObject object = (MyObject)view; //This object is placed in the Adapter.
object.getId(); //This method is inserted in my object or you can Override the method toString() to return the id of your query.
//Now you can send your ID to another Activity.
//Put your code here.
}
});
就是这样。我希望我能帮助你解决问题。如有任何问题,请在此处提出您的问题。
-
thiago L. Silva