[英]android gridview subitems start activity onclick
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);
}
}
I know this method but lets say for example i have 20 subitems so i need 20 activities !! 我知道这种方法,但是可以说例如我有20个子项,所以我需要20个活动! how do i make it so only one activity is passed in but the data inside it changes depending on the subitem clicked ( by the data inside it i mean simple Textviews ) sorry my english is very bad
我如何做到这一点,所以只有一个活动被传入,但是其中的数据根据所单击的子项目而改变(通过其中的数据,我的意思是简单的Textviews)。
You can set a extra to the activity intent to indentify where you came from: 您可以对活动意图进行额外设置,以识别您的来源:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(this, second.class);
i.putExtra("position", position);
startActivity(i);
}
Then in your activity you can get the intent that like this: 然后在您的活动中,您将获得如下意图:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.