Android-如何从列表中获取listItems的索引

Android-如何从列表中获取listItems的索引,android,listview,android-listview,Android,Listview,Android Listview,我正在尝试获取列表项的索引,通过它我可以为另一个活动中的不同项设置不同的视图。这是我的密码 String description[] = {"inspiron","pavilion","macbook"}; /* i want this list on another activity after clicking listitem of first activity.*/ ArrayList<String> listDesc = new ArrayList<String&g

我正在尝试获取列表项的索引,通过它我可以为另一个活动中的不同项设置不同的视图。这是我的密码

String description[] = {"inspiron","pavilion","macbook"}; /* i want this list on another activity after clicking listitem of first activity.*/

ArrayList<String> listDesc = new ArrayList<String>();
String ArrayDesc[] = null;

ListView listViewDesc;

ArrayAdapter<String> listAdapterDesc;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

            super.onCreate(savedInstanceState);
            setContentView(R.layout.category);

            for(int i = 0;i<description.length;i++)
    {
        listDesc.add(description[i]);
    }

    ArrayDesc = (String[]) listDesc.toArray();

       listAdapterDesc=newrrayAdapter<String>this,android.R.layout.simple_spinner_item,ArrayDesc);
   listViewDesc.setAdapter(listAdapterDesc);
   public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
   {

      lstView.setOnItemClickListener(new OnItemClickListener()
                {
                 @Override
                public void onItemClick(AdapterView<?> a,View v, int i, long l)
                    {

                     /*here i want some code to extract list item index from list to set 
                     different lists according to the item click on another view.*/


                        Intent intent = new Intent(getApplicationContext(), categorySelected.class);
                        startActivity(intent);


                    }
                });

从你的评论中你说

请告诉我代码,如果单击列表中的任何项目,其索引号将在Toast中弹出

因此,请尝试下面的方法

public void onItemClick(AdapterView<?> a,View v, int i, long l)
{
Toast.makeText(getApplicationContext(),"Index of the item clicked is"+i,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), categorySelected.class);
startActivity(intent);
}

String item=Stringa.getItemAtPositioni;在项目中单击。你只需要索引使用I。因为你只需要位置,只需要在I中使用I。它是已单击项目在适配器中的位置。我希望在单击第一个活动的listitem后,在另一个活动中显示此列表。你想要这个项目还是索引?@Raghunandan我如何向你展示我的完整代码?因为这里有一个代码限制,问题是我的代码的前一部分正在正确执行,在添加您刚才告诉的代码后,没有向Emmolator提供任何输出。@user2113484有什么问题?不需要显示完整的代码。我猜你已经发布了相关部分。但是你是否使用了intent.putExtrakey,item;并检索categorySelected类中的字符串项?好的。现在我正在设置if/else,比如ifi==0{/*这里我想为另一个活动设置name和age,但是name和age都有自己的线性布局*/}@user2113484,这是一个不同的部分。我想我已经回答了你当前的问题question@user2113484它有用吗?如果它有助于将答案标记为已接受,并且有一个不同的问题,则通过在那里发布if-else代码来启动一个新的线程