繁体   English   中英

获取选定的项目名称-Android ListView

[英]Get selected item name - Android ListView

我有ListView ,其中有来自数据库的项目。

我的适配器是这样的:

    Cursor cur = myDb.getCategories();
    // Closing the cursor
    // DEPRECATED!!
    startManagingCursor(cur);

    // Set up mapping from cursor to view fields
    String[] fromFieldNames = new String[] { DBAdapter.KEY_ITEM_CAT };

    int[] toViewIDs = new int[] { R.id.tvItemCat };

    // Create adapter to map columns of DB to elements on UI
    myCursorAdapter = new SimpleCursorAdapter(this, // Context
                R.layout.category_layout, // Raw layout template
                cur, // Cursor (set of DB records)
                fromFieldNames, // DB column names
                toViewIDs // views ID to putt in list view
    );

    // Set the adapter for list view
    LVCat.setAdapter(myCursorAdapter);

getCategories():

    Cursor c = db.rawQuery("SELECT DISTINCT " + KEY_ITEM_CAT + " as " + KEY_ITEM_ID
            + ", " + KEY_ITEM_CAT + " FROM " + ITEMS_TABLE_NAME, null);

    if (c != null) {
        c.moveToFirst();
    }

    return c;

我想要当我单击某些项目时我应该得到项目名称。 我搜索了,发现以下代码:

public void onCatClick(){

    LVCat.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // TODO Auto-generated method stub

            String category = LVCat.getItemAtPosition(position).toString();
         // String category = parent.getItemAtPosition(position).toString(); --> This also I've tried, but the same result

            System.out.println("Cat: " + category);
        }
    });
}

对于其他人来说,这可行。 但是我没有得到项目名称,我得到这样的东西: System.out(23858): Cat: android.database.sqlite.SQLiteCursor@4197f3a0

那么,如何获得所选的商品名称?

parent.getItemAtPosition(position).toString()使用parent.getItemAtPosition(position).toString()来获取项目。

或使用它

TextView tv = (TextView) v.findViewById(R.id.tvItemCat);

String value = tv.getText().toString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM