Set click listeners for dynamically created images

I have activity in my application in which rss feeds are displayed and next to each image are the arrows of the RSS file.

I am new to android, any help would be appreciated.

I will explain what I do to display rss news ...

I have a separate xml xml layout for one rss. I set the id for the arrow image (which will move to the next action) in it, like iv_arrow_img, I repeat the news feeds that I get, and for each news feed I add a dummy view again and again ... my question is how will I distinguish between different arrow icons of the image ... because at the moment everyone has the same identifier ... I set onclick listeners to them below in my code

I 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++;
}

, . - , ...?

+3
2

, ( , , ), , ... findViewById... , , couse smthing :

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

... ListView

+2

, , , - . , XML android:id="+@id/XXXXXXX".

:

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 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

All Articles