简体   繁体   English

Android ListView:从其值获取项目的位置?

[英]Android ListView: get item's position from its value?

I'm new to Android and Java. 我是Android和Java的新手。 I was wondering if there is a way to find the position of a ListView item by its value. 我想知道是否有一种方法可以通过其值查找ListView项目的位置。

For example: 例如:
If I have an item in my ListView that displays the string "Product 1108" How would I get the position of "Product 1108"? 如果我的ListView中有一个显示字符串“ Product 1108”的项目,如何获得“ Product 1108”的位置?

private void updateListView() {
    final ListView lvCheckList = (ListView) findViewById(R.id.lvCheckList);
    Map<String, ?> keys = checkListData.getAll();

    ArrayList<String> checkListStrings = new ArrayList<String>();
    for (Map.Entry<String, ?> entry : keys.entrySet()) {
            if (entry.getKey().equals(currentList + entry.getValue())) {
                checkListStrings.add((String) entry.getValue());
            }
            if (entry.getKey().equals(currentList + entry.getValue() + "Checked")) {
                //set Item to checked / unchecked

            }
        }

        ArrayAdapter<String> checkListArrayAdapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_checked,
                checkListStrings);
                lvCheckList.setAdapter(checkListArrayAdapter);  
                }

This code gets the the the items stored in SharedPreferences and displays them in the ListView. 此代码获取存储在SharedPreferences中的项目并将其显示在ListView中。 Don't ask me why it works, it just does. 不要问我为什么会起作用,它会起作用。

Now see this line: 现在看到这一行:

if (entry.getKey().equals(currentList + entry.getValue() + "Checked")) {
                //set Item to checked / unchecked
    }

What I want to happen here is to set the list item to either checked or unchecked, but I don't think that it's possible because the listview has not been populated yet. 我要在这里发生的事情是将列表项设置为选中或未选中,但是我认为这是可能的,因为尚未填充listview。 So in the ArrayAdapter I need to sort through the items of the listview, find the item with whatever value I'm looking for, and get the position of that item so I can use this code: 因此,在ArrayAdapter中,我需要对listview的项目进行排序,找到具有我要查找的值的项目,然后获取该项目的位置,以便可以使用以下代码:

lvCheckList.setItemChecked(position, value);

Hopefully this made sense. 希望这是有道理的。

If you have a BaseAdapter you should have a something like an ArrayList storing items, which you could use the indexOf(obj) method on. 如果您有BaseAdapter ,则应该有一个类似ArrayList存储项,可以在其中使用indexOf(obj)方法。

If you're using an ArrayAdapter you could use getPosition(obj); 如果使用的是ArrayAdapter ,则可以使用getPosition(obj);

Here is an example of looping over an ArrayAdapter (let me know if you use something else): 这是循环遍历ArrayAdapter的示例(让我知道是否使用其他方法):

ArrayAdapter adapter = new YourCustomArrayAdapter();
for(int i = 0; i < adapter.getCount(); i++)
    System.out.println(String.format("String \"%s\" has an index of %d", adapter.getItem(i), i);

This does not use the method getPosition() because our loop feeds us a position that we get the item for. 这不使用方法getPosition()因为我们的循环向我们提供了我们要获得商品的位置。

You can do this 你可以这样做

Toast.makeText(getApplicationContext, "The position of the clicked item is " + position, Toast.LENGTH_SHORT).show();

in the listview onItemClickListener() that you will implement 在将要实现的listview onItemClickListener()

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

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