Android how to access views from my custom list if I use a CustomAdapter?

孤街醉人 提交于 2019-12-24 16:26:48

问题


In my application, I have ListView which is set by my CustomAdapter (i.e. ArrayAdapter). CustomAdapter has ViewHolder implemented in it which has references to all the views in my CustomList. Now my question is how can I access the views in my CustomList (like EditText in below code) as the class hierarchy is like

public class BaseActivityClass{
    ...
    class CustomAdapter{
        ...
        public class ViewHolder{
            EditText et;
            ...
            }
        }
    }

from my activity BaseActivityClass?


回答1:


You can give your View a unique id (View.setId()) or tag (View.setTag()) in your getView() method and then use findViewById() or findViewByTag() to use them




回答2:


In your Custom adapter

public View getView(int position,View convertView,ViewGroup parent){
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate("R.layout.yourcustomlistviewlayout ", parent, false);
EditText et=(EditText)row.findViewById(R.id.youredittext);
}

this way you can control your custom adapters views



来源:https://stackoverflow.com/questions/9373951/android-how-to-access-views-from-my-custom-list-if-i-use-a-customadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!