简体   繁体   English

Android:动态查找组件

[英]Android: Finding components dynamically

I'm developing an app which includes a ListView. 我正在开发一个包含ListView的应用程序。 The ListView items includes, among others components, a button. 除了其他组件外,ListView项还包括一个按钮。

The point is that this ListView may vary over time and I'm not sure how I can get these buttons to develop their action (it is always the same action, just adapted to the list view item data). 关键是该ListView可能会随时间变化,并且我不确定如何获取这些按钮来开发其动作(它始终是相同的动作,只是适应于列表视图项数据)。

Thanks for the help!! 谢谢您的帮助!!

PS: If someone is wondering, it is a map app. PS:如果有人想知道,那是一个地图应用程序。 The list includes some interesting places and I want to show their specifics locations when the button is pressed. 该列表包括一些有趣的地方,当我按下按钮时,我想显示它们的具体位置。

Inside your list's adapter Override getView() 在列表的适配器内部Override getView()

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.my_item_layout, null);
            }


                Button btn = (Button) v.findViewById(R.id.my_view_id);
                btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Do something when clicked
                }
            });

            return v;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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