【问题标题】:How to get CardView ID from its child TextView如何从其子 TextView 获取 CardView ID
【发布时间】:2020-05-13 14:18:58
【问题描述】:

您好,我需要在 TextView 中识别 mParent,确切地说是 CardViewRelativeLayout

我需要通过CardViewRelativeLayout的片段插入SQLite数据,其中包含许多CardView;所以我需要确定数据属于哪个布局或CardView数据,以便创建一个插入它们的循环。

我需要CardViewname 和RelativeLayoutname

【问题讨论】:

  • 嗨墨西哥人;有一个getParent()方法可以获取父视图
  • layout = getParent().toString ,这个结果是:android.widget.RelativeLayout{2371e4d VE..... ......ID 0,0-0,0 #7f0a0133 app :id/relap1},我需要名字 relap1
  • 你需要父布局的id吗?
  • 是的,我需要textview来自的布局名称,
  • 你可以从带有R.id.xx的布局中获取它,就像TextView本身的ID一样对吗?

标签: android sqlite textview fragment


【解决方案1】:

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

我假设 TextView 的 id 是 textview1CardView 的 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();

【讨论】:

  • @MexiCano 欢迎..您还可以将所有文本视图添加到数组中以方便访问它们..
  • 嗨,Zain,java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 androidx.cardview.widget.CardView in..... . " TextView textView1 = v4.findViewById(R.id.textView5);"....--------. " idcard = ((CardView) textView1.getParent()).getId();" ....-----....卡片视图在一个线性布局内
  • @MexiCano 你能分享你的布局吗
  • 是的,但是这里没有RelativeLayouts ..你能分享xml代码
  • 是的,这个示例布局:github.com/Luismx30/test/blob/master/textlayout
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 2019-10-21
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
相关资源
最近更新 更多