开发者

set click listeners for dynamically created imageviews

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...i have set onclick listeners to them below in my code

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;
  开发者_如何学运维      }
    });
    id++;
}

but i am not getting different tags for different news though they are different .. can any one tell me what i am doing wrong... ?


asuming that view is final(in other case, i think, you couldn't compile this) so it's pointing to last view that you create ... there is no need for using findViewById ... in event you got image view which couse it so try smthing like this:

ivArrowfwd.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent me) {
        ImageView iv = (ImageView)v;
        System.out.println("sdfsdf" + iv.getTag());
        return false;
    }
});

anyway ... you should consider using ListView instead


What you probably want is something like the following. You will have to find your Views by their id you gave them in the .xml file with android:id="+@id/XXXXXXX".

Your code will look something like this:

String id;
int resID;
ImageView views[] = new ImageView[NUM_OF_VIEWS];

for(int i = o; i < NUM_OF_VIEWS; i++){
    id = "ImageViewIdentifier" + i; // Do name their id's in the .xml file so that you can easily loop over them.
    resID = getResources().getIdentifier(resID, "defType", "defPackage"); // see here: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
    views[NUM_OF_VIEWS-1] = (ImageView) findViewById(resID);
    views[NUM_OF_VIEWS-1].setOnTouchListener(new OnTouchListener(){
            // DO Stuff here
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜