Search code examples
androidsqlitetextviewfragment

How to get CardView ID from its child TextView


Hi I need to identify mParent in TextView, exactly CardView and RelativeLayout

enter image description here

I need insert in SQLite data by CardView and RelativeLayoutof a fragment, which contains many CardView; so I need to identify which layout or CardViewthe data belongs to in order to make a cycle that inserts them.

I need CardViewname, and RelativeLayoutname


Solution

  • So, you've many CardViews, and each CardView has a wrapped-in TextView, and you want to facilitate accessing to all these card views from the TextViews.

    I'll assume the id of the TextView is textview1 and of the CardView is cardview1

    Then To get access to the ID of the parent CardView using its child TextView:

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

    Also note that, you'll never get cardview1 as a string as it's stored in the system as int value.

    To make sure you get the right ID, you do some check like

    if (((CardView) textView1.getParent()).getId() == R.id.cardview1)
        Toast.makeText(this, "Cardview", Toast.LENGTH_SHORT).show();