3

In my sample news application I have a view Flipper that display news title, you can see XML code of the ViewFlipper below.

<ViewFlipper android:id="@+id/pushFlipper"
        android:layout_width="fill_parent"
        android:layout_height="25dp"
        android:background="#90333333"
        android:flipInterval="4000"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dip"
        >

    </ViewFlipper>

Dynamically I add TextViews to this ViewFlipper in order to display the news title in animated form. Here is the code where I add TextViews to the Flipper.

Now, the issue I am facing is how to set listener on the dynamic TextViews, that I could display the appropriate news descriptionin new Activity of the title that has been displayed on the textview. Please help me in this respect, your help would be greatly appreciated.

pushFlipper = ((ViewFlipper) this.findViewById(R.id.pushFlipper));
            pushFlipper.startFlipping();
try
            {
                RSSdata = new RSSHandler();
                for (int i = 2; i < RSSdata.getTitle().size(); i++)
                {
                    TextView tvNewsRSS = new TextView(this);
                    tvNewsRSS.setText(RSSdata.getTitle().get(i).toString());
                    tvNewsRSS.setTextColor(Color.parseColor("#FFFFFF"));
                    tvNewsRSS.setTextSize(14);
                    tvNewsRSS.setGravity(Gravity.CENTER_VERTICAL);

                    pushFlipper.addView(tvNewsRSS);

                }     
            }
            catch(Exception e)
            {
            }
user1703737
  • 533
  • 1
  • 11
  • 25
  • Here is a solution: http://stackoverflow.com/questions/3813108/listener-for-viewflipper-widget-flipping-events You can get index of child easily – Sherif elKhatib Feb 04 '13 at 14:08
  • Can you explain your issue better? do you want to implements a listener in the dynamic text to do what? – Marckaraujo Feb 04 '13 at 14:24
  • The textviews display news titles, by clicking on the textView I want to display news description of the displaying news title. – user1703737 Feb 04 '13 at 14:49

1 Answers1

4

One of the solution can be getting newsId on click of item from textview tag and then using this newsId in next activity to fetch and show appropriate news.

Code:

try
    {

        for (int i = 0; i < 10; i++)
        {
            TextView tvNewsRSS = new TextView(this);
            tvNewsRSS.setText(RSSdata.getTitle().get(i));
            tvNewsRSS.setTextColor(Color.parseColor("#FFFFFF"));
            tvNewsRSS.setTextSize(14);
            tvNewsRSS.setTag(RSSdata.getId().get(i));
            tvNewsRSS.setGravity(Gravity.CENTER_VERTICAL);
            tvNewsRSS.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    String newsId = v.getTag().toString();

                    // Pass this newsId to next activity via intent putExtra.

                }
            });
            pushFlipper.addView(tvNewsRSS);

        }     
    }
    catch(Exception e)
    {
    }
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38