1

我正在尝试在我的应用中整理收藏夹。我有一个包含三列的数据库:

_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)位置0ViewFavsActivity显示了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

项目

4

2 回答 2

2

真正的问题是您使用了两个不同的光标。你position == 0从:

cursor = database.query(TABLE_NAME, null, COLUMN_STAR + " = ?",
            new String[] { "yes" }, null, null, null);

但随后要求职位0

setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC },
            null, null, null, null, null);

所以很有可能你会得到不同的结果。
Bundle也可以保存任何原始数据类型和一些更复杂的数据类型。因此,您无需将数字转换为字符串,然后再转换回其原始数据类型...

安全的方法是使用行的 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);

(注意:如有疑问,请始终使用?andquery()selectionArgs参数来防止 SQL 注入攻击,但是您正在使用long作为索引检索的 a,因此在这种情况下您是安全的。)

于 2012-09-26T23:05:05.247 回答
0

这里的问题是您的光标只产生一个对象。我相信是 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();
        }
    });

解决方案:

您需要将方法中的视图投射到放置在适配器中的对象。前任:

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.

        }
    });

就是这个 我希望我能帮助你解决我的问题。有任何问题请在这里提出您的问题。

--

蒂亚戈·L·席尔瓦

于 2012-09-26T22:05:38.137 回答