Search code examples
androidandroid-studioandroid-fragmentsonclicklistener

cannot be cast to android.view.View$OnClickListener


I have a fragment for displaying data with many dynamic textViews,when implements setOnClickListener for dynamic view get this error:

com.Package.activity.DashboardActivity cannot be cast to android.view.View$OnClickListener

Activity :DasgboardActivity : load fragment in frameLayout

  getSupportFragmentManager().beginTransaction().replace(R.id.container,new TestFragment()).commit();

Fragment

public class TestFragment extends Fragment implements View.OnClickListener{

  CreateView createView;
List<Model> listData=new ArrayList();
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
  createView = new CreateView();
  listData=db.getAllData();
  createView.makeView(getActivity,listData);
}


    @Override
    public void onClick(View v) {}

}

CreateView class: for creating dynamic views:

  public View makeView(Context context, List<Model> words_List) {
        LinearLayout linearLayout = new LinearLayout(context);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            for (Model w : words_List) {

                TextView textView = new TextView(context);

                textView.setText(w.getW_text()+"");
                textView.setId(w.getW_id());
                textView.setTag(w.getW_id());


                    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));


       textView.setOnClickListener((View.OnClickListener) context);


                linearLayout.addView(textView);
            }


        return linearLayout;
    }

in this line i got error : textView.setOnClickListener((View.OnClickListener) context);

My code completely working on activity but in fragment i have error

How i must fix my bug?


Solution

  • You can create an instance of OnClickListener interface in CreateView class like this :

       public class CreateView {
    
            View.OnClickListener onClickListener = null;
    
            public CreateView(View.OnClickListener  listener){
                this.onClickListener = listener ; 
            }
    
    
            public View createLabelForMainWordsInOneLine(Context context, List<Model> words_List) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    
                for (Model w : words_List) {
    
                    TextView textView = new TextView(context);
    
                    textView.setText(w.getW_text() + "");
                    textView.setId(w.getW_id());
                    textView.setTag(w.getW_id());
    
    
                    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));
    
    
                    textView.setOnClickListener(onClickListener);
    
    
                    linearLayout.addView(textView);
                }
    
    
                return linearLayout;
            }
        }
    

    Then , You can instanciate CreateView like this :

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
      createView = new CreateView(this);
      listData=db.getAllData();
      createView.makeView(getActivity,listData);
    }