【问题标题】:Getting item position in ListView then use the if / else clause or switch statement在 ListView 中获取项目位置然后使用 if / else 子句或 switch 语句
【发布时间】:2013-11-18 07:08:15
【问题描述】:

我有一个 listView,我想知道如何获取 ListView 中所选项目的位置,以便我可以在 OnItemClick 方法下使用此代码。

string selected = listView.getItemPosition().toString()

那么我可以使用 if / else 子句或 switch 说:

if (selected.positionEquals("0")) {
        Intent i = new Intent(this, WebViewActivity.class);
        i.putExtra("keyHTML", "file:///android_asset/page1.html");
        startActivity(i);

我希望我说得通,请注意,listView.getItemPosition().toString() 和 selected.positionEquals 是我编造的,因此您可以了解我想要什么。

谢谢!

编辑

我刚刚从另一个问题中看到了这个,我在问这个之前搜索了这个,但这只是突然出现了。你认为它适用于我的情况吗?

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

if(position == 1)
{

     Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
         startActivityForResult(myIntent, 0);
}

if(position == 2)
{

     Intent myIntent =  new Intent(YourActivity.this, ThirdActivity.class);
         startActivityForResult(myIntent, 0);
}
    }
  });

【问题讨论】:

  • 将此代码写入listview的onItemSelected监听器中。然后它会工作
  • 不,你没有任何意义。请更清楚您的问题。你不使用你的代码得到这个职位吗?你在onItemSelecteListener写过代码吗?
  • 你得到位置作为onItemClick中的参数,它给出了listitem的索引

标签: java android listview android-listview


【解决方案1】:

onItemClick的一个参数中,有一个被点击项目的位置值(第三个参数)。您不需要以程序方式获取它,只需使用该值即可了解

public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { //check third argument it automatically get the selected item POSITION
    // TODO Auto-generated method stub
    Intent i;
    switch (position)
    {
    case 0: // first one of the list
        i = new Intent(this, anActivity.class);
        startActivity(i);
        break;
    case 1: // second one of the list.
        i = new Intent(this, anActivity2.class);
        startActivity(i);
        break;
       // and so on...
    }

}

【讨论】:

  • 很高兴帮到你,如果你觉得有用,请在右边打勾:)
  • @Coderji 我对我的ListView应用了一个适配器过滤器,我发现当ListView被过滤并且正确的文本被带到第一个位置时,当它被点击时,它会打开活动为第一个位置,而过滤后的文本的位置为 12。你怎么看?
  • 也许你在getViewgetItem 函数中遗漏了一些东西,请参考这个question 它有你同样的问题,希望它有你同样的解决方案。
  • @Coderji 你能不能这么温柔/乐于助人帮我看看这里,谢谢stackoverflow.com/questions/20052214/…
  • 我会尽力帮助你的。
【解决方案2】:

试试这个代码:

    listview.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });

这里的“int position”会给出选中项的索引值。

【讨论】:

    【解决方案3】:

    请看下面的代码:-

    listView.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int index,
                        long arg3) {
                    if (index == 0)) {
            Intent i = new Intent(this, WebViewActivity.class);
            i.putExtra("keyHTML", "file:///android_asset/page1.html");
            startActivity(i);
                }
            });
    

    【讨论】:

    • 非常有用.. 使用了这个...它让我不必强调因为我在另一个活动中有我想要的 if 语句,我只是复制粘贴和替换,只需要修复数字,谢谢。
    【解决方案4】:

    使用下面的代码Toast 已打印所选项目的位置:-

    listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    Toast.makeText(getApplicationContext(),"Position of Selected Item is :-"+position,Toast.LENGTH_LONG).show(); 
    }
    });
    

    【讨论】:

      【解决方案5】:

      是的,以这种方式为你工作

          listView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      
          if(position == 1)
          {
      
               Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
               startActivityForResult(myIntent, position);
          }
      
          if(position == 2)
          {
      
               Intent myIntent =  new Intent(YourActivity.this, ThirdActivity.class);
                   startActivityForResult(myIntent, position);
          }
      
       }    
       });
      

      也切换到将为您工作, 如果你想使用类似的字符串进行比较

      string selected = listView.getItemPosition().toString()
      

      那就点赞

       if (selected.equalsIgnoreCase("0")){
      
        // do work here 
      
         }else if(selected.equalsIgnoreCase("1")){
      
            // do work here 
        }
      

      以此类推。

      【讨论】:

      • 有用...非常有用,现在我知道 if (selected.equalsIgnoreCase("0")){
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多