public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent i = new Intent(this,second.class);
startActivity(i);
} else if (position == 1) {
Intent i1 = new Intent(this,second.class);
startActivity(i);
}
}
我知道这种方法,但假设例如我有 20 个子项目,所以我需要 20 个活动! 我如何使它只传入一个活动,但其中的数据会根据单击的子项而变化(通过它里面的数据,我的意思是简单的文本视图( 对不起,我的英语很差
您可以为活动意图设置额外的内容,以识别您来自哪里:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(this, second.class);
i.putExtra("position", position);
startActivity(i);
}
然后在您的活动中,您可以获得这样的意图:
int position;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //if your activities have different layouts depending the given position, you can move this line inside the switch function
...
if(getIntent().getExtras() != null) {
position = getIntent().getExtras().getInt("position", 0);
}
switch(position){
case 1:
//do something
break;
case 2:
//do another thing
break;
default:
//default behaviour
break;
}
}