Ritt
Ritt

Reputation: 3349

Using setOnClickListener() in dynamically generated TextView

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.

Upvotes: 0

Views: 544

Answers (1)

SuperFrog
SuperFrog

Reputation: 7674

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);
    }
}

Upvotes: 2

Related Questions