0

This is the TextView Layout.

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/trailer"
    android:paddingTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="16sp"/>

This is the main root layout.

     <LinearLayout
        android:id="@+id/review_layout"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>

And this piece of code add the textViews as childView of the above linear layout, based on total number of response from server.

    @Override
    public void addData(List<MovieVideoData> movieVideoDataList) {
        trailerLinearLayout.removeAllViews();
        List<TextView> textViews = new ArrayList<TextView>();
        for (MovieVideoData movieVideoData:movieVideoDataList){
            View view = getActivity().getLayoutInflater().inflate(R.layout.trailer_text_view,trailerLinearLayout,false);
            ((TextView)view.findViewById(R.id.trailer)).setText(movieVideoData.getName());
            textViews.add(((TextView)view.findViewById(R.id.trailer)));
            trailerLinearLayout.addView(view);
        }
    }

Now, for each TextView objects which was added in the Linear Layout, on the click of those i want to open an youtube video link specific to that textView.

I was wondering, how should i use the setOnClickListener() on each textView as id's of all are same.

Any suggestions, would be appreciated.

I added this piece of code and it's working fine.

private void watchYoutubeVideo(String key){
        try{
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + key));
            startActivity(intent);
        }catch (ActivityNotFoundException ex){
            Intent intent=new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://www.youtube.com/watch?v="+key));
            startActivity(intent);
        }
    }


textView.setText(movieVideoData.getName());
            trailerLinearLayout.addView(view);
            final String key = movieVideoData.getKey();
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    watchYoutubeVideo(key);
                }
            });

On click of each textview, it's opening its respective link on youtube, which is fine.

What i am not sure of is, after movieVideoDataList completes looping, and then on click of text view, how it remembers the specific key, as it inside the loop.

Any type of clarification on this would be appreciated.

Neela
  • 1,328
  • 4
  • 23
  • 45
Ritt
  • 3,181
  • 3
  • 22
  • 51
  • the easy way is to assign a different instance to each textview – Blackbelt Nov 01 '15 at 19:50
  • I think you could set new listener to every one or if the logic is identical maybe you could attach to them one listener that is defined in your activity – kirotab Nov 01 '15 at 19:51
  • @Blackbelt Thank you for replying, i thought about it. But, what if the size of my arrayList is say 10+. In that case if have to set listener for all objects and my code will grow unnecessarily. I was wondering, if there is any better solution. – Ritt Nov 01 '15 at 19:55
  • Possible duplicate of [How to add onclicklistener to dynamically generated text view?](http://stackoverflow.com/questions/8722595/how-to-add-onclicklistener-to-dynamically-generated-text-view) –  Nov 01 '15 at 19:56
  • @kirotab Thank for replying. Are you suggesting, to call one common method on click of text view? – Ritt Nov 01 '15 at 19:57
  • I was thinking something along the lines of your containing class `implements OnClickListener` and `@Override public void onClick(View v)` and when you set the listener `textView.setOnClickListener(this)` – kirotab Nov 01 '15 at 20:31

1 Answers1

2

I might be missing something, but why not just do it like this:

@Override
public void addData(List<MovieVideoData> movieVideoDataList) {

    trailerLinearLayout.removeAllViews();

    List<TextView> textViews = new ArrayList<TextView>();

    for (MovieVideoData movieVideoData:movieVideoDataList){

        View view = getActivity().getLayoutInflater().inflate(R.layout.trailer_text_view,trailerLinearLayout,false);
        TextView tv = (TextView)view.findViewById(R.id.trailer);
        tv.setText(movieVideoData.getName());
        textViews.add(tv);

        tv.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
              //set the url of your video and action you want to perform
           }
         }); 

        trailerLinearLayout.addView(view);
    }
}
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • Thank you, it worked. Can you look into my edited question. – Ritt Nov 01 '15 at 20:10
  • You assign a different key to each listener, don't you? – SuperFrog Nov 01 '15 at 20:12
  • yes, so is it like, when you set onclickListener on each new textview with their specific key, the key will be stored as a property of onclicklistener object? – Ritt Nov 01 '15 at 20:14
  • That's actually the downside to this solution, because you're creating multiple listeners. You can alternatively implement View.OnClickListener in your activity, and then use onClick method for all of your buttons. You will need either to generate an id (which if you target at sdk < 17 might be tricky), or use tag, and then switch by it. – SuperFrog Nov 01 '15 at 20:21
  • okay, how you get the respective key of each textview, if i go with the above approach like you said? – Ritt Nov 01 '15 at 20:28
  • A very simple solution, which I didn't try, can be setting the key as tag of your view: tv.setTag(key); And later use: tv.getTag().toString(); to get the needed key. – SuperFrog Nov 01 '15 at 20:32
  • Look at this one for using tags: http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – SuperFrog Nov 01 '15 at 20:34
  • Thanks mate. This solution is also working and is more efficient, setting tag in the view and then process. – Ritt Nov 01 '15 at 20:44
  • No problem :) Regarding efficiency (memory-wise) I'm not sure, because we're adding the tags, but that's a bit hard to say. – SuperFrog Nov 01 '15 at 20:55
  • Okay, but creating new listener object for each new textview is avoided though. we are just adding one more property which is setTag. Not, sure how much memory it takes as compared to new listener. – Ritt Nov 01 '15 at 21:03