如何从其子TextView中获取CardView ID

问题描述 投票:0回答:1

你好,我需要在 TextView确切地说 CardViewRelativeLayout

enter image description here

我需要插入 SQLite 数据按 CardViewRelativeLayout的片段,其中包含许多 CardView所以我需要确定哪种布局或 CardView的数据所属的循环,以便插入它们。

我需要 CardView名称,以及 RelativeLayout名称

android sqlite textview fragment
1个回答
1
投票

所以,你有很多CardViews,每个CardView都有一个封装的TextView,你想方便地从TextViews中访问所有这些卡片视图。

我假设TextView的id为 textview1CardViewcardview1

然后要获得对父体ID的访问权 CardView 使用其子女 TextView:

TextView textView1 = findViewById(R.id.textview1);
int id = ((CardView) textView1.getParent()).getId();

还请注意,你永远不会得到 cardview1 作为一个字符串,因为它在系统中存储为int值。

为了确保得到正确的ID,你可以做一些检查,如

if (((CardView) textView1.getParent()).getId() == R.id.cardview1)
    Toast.makeText(this, "Cardview", Toast.LENGTH_SHORT).show();
© www.soinside.com 2019 - 2024. All rights reserved.