2017-03-31 64 views
-1

我有很多動態textViews,當工具setOnClickListener動態視圖得到這個錯誤顯示數據的片段:

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

活動:DasgboardActivity:負載片段的FrameLayout

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

片段

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的類:用於創建動態視圖:在此行

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

我得到錯誤: textView.setOnClickListener((View.OnClickListener)上下文);

我的代碼完全工作的活動,但在片段我有錯誤

如何,我必須解決我的錯誤?

回答

0

可以在CreateView的類中創建OnClickListener接口的一個實例是這樣的:

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

然後,你可以instanciate CreateView像這樣:

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

謝謝你。問題解決:) –

+0

不客氣^^ –

0

它是實現View.OnClickListener的片段,而不是活動。 而是執行此操作:

public class TestFragment extends Fragment{ 

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


    View.OnClickListener textViewClickListener = new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //your listener code here 
     } 
    }; 
} 

而當你需要將OnClickListener設置TextView的,使用

textView.setOnClickListener(textViewClickListener); 
+0

如何通過參數傳遞textViewClickListener到CreateView類? –

+0

我明白你在說什麼。如果CreateView類獨立於片段並且無法訪問OnClickListener,請在CreateView類中定義textViewClickListener,而不是在片段內部。 –

相關問題