其他分享
首页 > 其他分享> > android – 如何使用OnItemClickListener根据单击的项目启动新意图?

android – 如何使用OnItemClickListener根据单击的项目启动新意图?

作者:互联网

我希望能够使用Intent类启动一个新活动.我知道如何使用以下代码行启动活动:

Intent myIntent = new Intent(v.getContext(), bylocationactivity.class);

startActivityForResult(myIntent, 0);

但是,我如何指定单击了哪个项目?所以,当我点击“按位置”时,我可以启动bylocationactivity.class等等?

public class bonesactivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        ListView boneslist;
        String categorieslist[]={"Alphabetically","By Location","Specialty Tests"};
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boneslayout);
        boneslist=(ListView)findViewById(R.id.boneslayout);
        boneslist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , categorieslist));
        boneslist.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id)
            {

            }
        });
    }      
}

解决方法:

代码演示了多个按钮的单个OnItemClick Listner

您可以使用相同的物品作为物品!

//在所有6个按钮的单击侦听器上

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    //int clickedButtonIs;

    if (v == button1)
    {
        // call intent 1;
    }
    else if (v == button2)
    {
        // call intent 2;
    }
    else if (v == button3)
    {
        // call intent 3;
    }
    else if (v == button4)
    {
        // call intent 4;
    }
    else if (v == button5)
    {
        // call intent 5;
    }
    else if (v == button6)
    {
        // call intent 6;
    }
}

标签:listviewitem,onitemclicklistener,android,string,listview
来源: https://codeday.me/bug/20190726/1538877.html