开发者

How to get View/Widget from getItemAtPostion method, android?

开发者 https://www.devze.com 2023-02-09 13:21 出处:网络
How to use gallery.getItemAtPosition(position) to get the view object like bel开发者_开发知识库ow.

How to use gallery.getItemAtPosition(position) to get the view object like bel开发者_开发知识库ow.

Object obj = gallery.getItemAtPosition(position);
View v = (View)object;
ImageView imgview = (ImageView)v.findViewById(R.id.imageId);

But if i use like that it is giving class cast exception.


Typically I use it like this, on select event

public void onItemSelected(AdapterView arg0, View v, int position,
            long arg3) {
        //here v is the imageview which is selected.
                //In your case u might want something like this.
                ImageView img = (ImageView) arg0.getChildAt(position); //To get the view
}


That source code does not make much sense, or vital information is missing. Have you connected a SpinnerAdapter to the gallery? What type of objects do the adapter return?

I assume all objects in the gallery are of the same type?

Object obj = gallery.getItemAtPosition(position); 

You should cast to this object type directly.

TheType obj = (TheType)gallery.getItemAtPosition(position);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号