Cardview onclick打开一个新活动 [英] Cardview onclick opens a new activity

查看:125
本文介绍了Cardview onclick打开一个新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击卡片可以打开一个活动,然后单击另一个卡片可以打开另一个活动,依此类推.有什么方法可以创建一个活动并识别所点击卡片的ID并显示其相应数据?

Clicking a card opens an activity then clicking on another card opens another activity and so on. Is there any way to create just one activity and recognize id of the card clicked and show its corresponding data ??

推荐答案

您可以使用Intent.putExtra发送ID,然后在活动中使用Intent.getIntExtra获取ID,并在活动中提供数据

you can send id with Intent.putExtra and then get it with Intent.getIntExtra in your activity and provide your data in activity

这里是一个示例,如果您使用ListView,则将ID和索引发送到MyActtivity:

Here is an example that sending id and index to MyActtivity if youre using ListView:

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getApplicationContext(),MyActivity.class);
        intent.putExtra("id",view.getId());
        intent.putExtra("index",position);
        startActivity(intent);
    }
};

您可以像这样在MyActivity中检索它:

And you can retrieve it in MyActivity like this:

Intent intent = new Intent(getIntent());
int id = intent.getIntExtra("id",0);
int index = intent.getIntExtra("index",0);

这篇关于Cardview onclick打开一个新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆