开发者

click listeners for dynaically added imageview

开发者 https://www.devze.com 2023-03-13 13:40 出处:网络
I have an activity in my app which displays rss feeds and next to each rss feed arrow image is attached.

I have an activity in my app which displays rss feeds and next to each rss feed arrow image is attached.

I am new to android any help will be appreciated.

i shall explain what i am doing to display rss news ...

i have a seperate dummy xml layout for a single rss.. i have set id for arrow image (which will navigate to the next activity) in it as iv_arrow_img i am iterating over the news feeds i get and for each news feed i am adding the dummy view again and again...my question is how will i distinguish between different image arrow's ids .. because for now all are having the same id... how will i set onclick listeners to them ???

..... @devA taking ur suggestions i have wrote the code Iterator itr = data.iterator(); int i =0; while (itr.hasNext()) { NewsPostDTO newspostdto = itr.next();

        view = inflater.inflate(R.layout.rl_news_item, null);
        lnContentView.addView(view, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);


        ivArrowfwd = (ImageView) view.findViewById(R.id.iv_arrowfwd);

        tvNewsHeading.setText(newspostdto.getFeaturedDesc());
        tvNewsContent.setText(newspostdto.getDate() + " - " + newspostdto.getTitle());
        ivArrowfwd.setId(id);
        ivArrowfwd.setTag(newspostdto);
        ivArrowfwd.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                               System.out.println("sdfsdf" +(ImageView) view.findViewById(id).getTag());

                return false;
            }
        开发者_如何学JAVA});
        id++;
    }

but i am not gettng different tags for each news thought they are unique .. can u tell me where i am doing wrong... ?


There are different ways to achieve this, Since its not clear what layouts you are using and how you are attaching the view to the layouts,

One way would be this, When you are populating view dynamically, use a method called setTag("uniqueid") on the view,

unique id may be url or something else which is unique to the view and you store them in array, Once views are populated, iterate the array , use findViewWithTag("uniqueid") and set the listener.


then there is no issue at all..

`image.setOnTouchListener(new OnTouchListener(){

@Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
//start your activity..
})

` to image.. with the current reference and inside it startActivity.. no need to have diff id's..

0

精彩评论

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