3

我希望能够使用 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)
            {

            }
        });
    }      
}
4

3 回答 3

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

});

于 2012-05-15T23:57:59.517 回答
2

演示多个按钮的单个 OnItemClick 列表器的代码

您可以将其用于您所说的物品!

// 所有 6 个按钮的 On Click Listener

@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;
    }
}
于 2012-05-16T05:49:07.247 回答
0

您可以使用position参数 inonItemClick从 categoriesList 数组中获取所需的字符串。所以:

 String category = categoriesList.get(position);

不过,可能必须使 categoriesList 成为成员变量。

于 2012-05-15T23:52:01.043 回答