我目前正在努力设置与我在gridView中点击的项目相关的OnItemClickListener。
以下代码打印出每个项目的索引位置。但相反,我希望它开始一项新活动,具体取决于点击的项目。
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new CustomGridViewAdapter(this, 0, gridArray));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(NonIllustratedHolder.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
我显然需要为每个索引创建一个startActivity,但是我该如何编写代码呢?有人能指出我正确的方向吗?
由于
答案 0 :(得分:0)
你的代码
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new CustomGridViewAdapter(this, 0, gridArray));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(NonIllustratedHolder.this, "" + position,
Toast.LENGTH_SHORT).show();
// function call the activity according to index
startActivity(position);
}
});
功能启动活动
public void startActivity(int index)
{
if(index==0)
{
//startFirstActivity
}
else if(index==1)
{
//startSecondActivity
}
//so on upto the number of item you have
}