我正在尝试在我的应用中整理收藏夹。我有一个包含三列的数据库:
_id | name | description | star
1 London some desc.
2 Berlin some desc.
3 Paris some desc. yes
我想在列表视图中显示收藏夹。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上的ViewFavsActivity