gpt4 book ai didi

java - android gridview子项启动 Activity onclick

转载 作者:行者123 更新时间:2023-12-02 13:37:06 24 4
gpt4 key购买 nike

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 个 Activity !我该如何做到这一点,以便只传入一个 Activity ,但其中的数据根据​​单击的子项而变化(其中的数据我指的是简单的 Textviews )抱歉我的英语非常糟糕

最佳答案

您可以为 Activity Intent 设置额外的内容来识别您来自哪里:

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent i = new Intent(this, second.class);
i.putExtra("position", position);
startActivity(i);
}

然后在你的 Activity 中你可以得到这样的 Intent :

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

关于java - android gridview子项启动 Activity onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42930570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com