简体   繁体   中英

android get id from ViewGroup

I have an ArrayAdapter with a button:

 public View getView(final int position, @Nullable final View convertView, @NonNull final ViewGroup parent)

 btnClickMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)

and this is what view.getParent().getParent() gives

android.widget.ListView{7d85545 VFED.VC.. .F....ID 16,70-704,1008 #7f0800cc app:id/tray_tab1}

How can i get app:id/tray_tab1 or tray_tab1 ?

Like this

  int parentId = ((View) view.getParent().getParent()).getId();

And now you can compare id with R.id like

 if (parentId == R.id.tray_tab1) {
       // Do what ever you want
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM