在ListView中获取项目位置然后使用if / else子句或switch语句

时间:2013-11-18 07:08:16

标签: java android listview android-listview

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

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

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

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);
}
    }
  });

5 个答案:

答案 0 :(得分:3)

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...
    }

}

答案 1 :(得分:0)

试试这段代码:

    listview.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });

这里“int position”将给出所选项目的索引值。

答案 2 :(得分:0)

请参阅以下代码: -

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);
            }
        });

答案 3 :(得分:0)

使用下面的代码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(); 
}
});

答案 4 :(得分:0)

是的,这样为你工作

    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 
  }

结束等等。