【问题标题】:How to get the position of a row in ListView without using OnItemClickListener如何在不使用 OnItemClickListener 的情况下获取 ListView 中一行的位置
【发布时间】:2014-11-02 20:16:56
【问题描述】:

我有一个带有 SQLite 数据库和 ListView 的应用程序,它使用 SimpleCursorAdapter 映射数据库。我使用 TextWatcher 用条形码扫描仪读取了一个字符串。现在我想在 ListView 中挑选出扫描的字符串并将相关的行放在最上面。因此,我认为我需要行的位置。如何获取已扫描字符串在 ListView 中的位置?或者有其他方法可以管理吗?

【问题讨论】:

  • 你可以通过你的适配器做到这一点

标签: android listview


【解决方案1】:
//mAdapter is your listview adapter
public int getRowInListView(String barcodedString){
    for(int i = 0; i < mAdapter.getCount(); i++){
        String s = mAdapter.getItem(i).toString();
        //if you have a custom object in you adapter instead use this 
        String s = ((MyObject)mAdapter.getItem(i)).getBarcode();

        if(s.equals(barcodedString))
            return i;
    }

    return -1
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    相关资源
    最近更新 更多