我希望能够使用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)
{
}
});
}
}
答案 0 :(得分:6)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = null;
switch(position) {
case 1:
intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
break;
case 2:
intent = new Intent(getApplicationContext(), Activity3.class);
startActivity(intent);
break;
default:
}
}
});
答案 1 :(得分:2)
演示单个项目的代码单击多个按钮的监听器
您可以将其用作物品!
//点击所有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;
}
}
答案 2 :(得分:0)
您可以使用position
中的onItemClick
参数从categoriesList数组中获取所需的字符串。所以:
String category = categoriesList.get(position);
可能必须使categoriesList成为成员变量。