2016-05-17 5 views
0

ユーザがリストアイテムをクリックするとポップアップウィンドウを表示したいと思います。私はinitiatePopWindow()を私の断片のアダプタクラスに使用しています。 findViewByIdConfirmActivityを解決するにはどうすればよいですか?アダプタクラスのConfirmActivityとfindViewByIdを解決できません

private void initiatePopupWindow() { 
     try { 
      //We need to get the instance of the LayoutInflater, use the context of this activity 
      LayoutInflater inflater = (LayoutInflater) ConfirmActivity.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      //Inflate the view from a predefined XML layout 
      View layout = inflater.inflate(R.layout.popup_addword, 
        (ViewGroup) findViewById(R.id.popup_element)); 
      // create a 300px width and 470px height PopupWindow 
      pw = new PopupWindow(layout, 300, 470, true); 
      // display the popup in the center 
      pw.showAtLocation(layout, Gravity.CENTER, 0, 0); 

      TextView WordPop = (TextView) layout.findViewById(R.id.textView2); 
      TextView PartsPop = (TextView) layout.findViewById(R.id.textView4); 
      TextView DescPop = (TextView) layout.findViewById(R.id.textView6); 
      TextView SentPop = (TextView) layout.findViewById(R.id.textView8); 
      WordPop.setText(list.getWord()); 
      PartsPop.setText(list.getParts()); 
      DescPop.setText(list.getDesc()); 
      SentPop.setText(list.getDesc()); 
      ImageButton cancelButton = (ImageButton) layout.findViewById(R.id.imageButton); 
      //makeBlack(cancelButton); 
      cancelButton.setOnClickListener(cancel_button_click_listener); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

答えて

0

アダプタインスタンスの作成中に、フラグメントからアクティビティコンテキストをアダプタに渡すことができます。

アダプターには既にコンテキストがあります。 これを試してください - 断片の

LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//Inflate the view from a predefined XML layout 
    View layout = inflater.inflate(R.layout.popup_addword,(ViewGroup)this.activity.findViewById(R.id.popup_element)); 
+0

これは初めての例です。 –

+0

アダプタをインスタンス化するコードを送信します。 –

+0

db =新しいWordBookHandler(getActivity()); wordList = db.getAllWords(); アダプタ=新しいWordBookAdapter(getActivity()、wordList); listView.setAdapter(adapter); –

0

getActivity()メソッドを、このようなアダプタにコンテキストを渡すために使用することができるようにコンテキストを返す - パラメータとしてコンストラクタにアダプタ

初回通過文脈。その後、アダプタのコンストラクタで、その後

//global 
Context context; 

public YourAdapter(Context context,YourOtherParameters...) { 
    this.context = context; 
} 

のようになります

YourAdapter adapter = new YourAdapter(getActivity(),YourOtherParameters...); 

ビューを膨らませるために、このコンテキストを使用する - これは、フラグメントがようにフラグメントにアダプタを初期化getActivity()メソッドを使用することによって行うことができます。

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.popup_addword,(ViewGroup)context.findViewById(R.id.popup_element)); 
関連する問題