How to set Click Listener to text view from viewFlipper side in Android?

In my sample news application, I have a Flipper displaying the title of the news, you can see the ViewFlipper XML 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 to display the news heading in an animated form. Here is the code where I add TextViews to Flipper.

Now the problem is how to set a listener on the dynamic TextViews so that I can display the relevant news description in a new header action that has been rendered as text. Please help me in this regard, 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)
            {
            }

      

+3


source to share


1 answer


One solution would be to fetch news when an element is clicked from the textview tag, and then use that newsId in the next step to retrieve and display the relevant 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)
    {
    }

      

+4


source







All Articles