如何从其子 TextView 获取 CardView ID
How to get CardView ID from its child TextView
您好,我需要在 TextView
中识别 mParent,正好是 CardView
和 RelativeLayout
我需要通过 CardView
和 RelativeLayout
的片段插入 SQLite
数据,其中包含许多 CardView
;所以我需要确定数据属于哪个布局或 CardView
以便创建一个循环来插入它们。
我需要 CardView
姓名,以及 RelativeLayout
姓名
所以,您有很多 CardView,并且每个 CardView 都有一个包装的 TextView,并且您希望方便地从 TextView 访问所有这些卡片视图。
我假设 TextView 的 id 是 textview1
而 CardView
的 id 是 cardview1
然后使用其子项 TextView
访问父项 CardView
的 ID:
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();
您好,我需要在 TextView
中识别 mParent,正好是 CardView
和 RelativeLayout
我需要通过 CardView
和 RelativeLayout
的片段插入 SQLite
数据,其中包含许多 CardView
;所以我需要确定数据属于哪个布局或 CardView
以便创建一个循环来插入它们。
我需要 CardView
姓名,以及 RelativeLayout
姓名
所以,您有很多 CardView,并且每个 CardView 都有一个包装的 TextView,并且您希望方便地从 TextView 访问所有这些卡片视图。
我假设 TextView 的 id 是 textview1
而 CardView
的 id 是 cardview1
然后使用其子项 TextView
访问父项 CardView
的 ID:
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();