2016-04-01 21 views
2

私はヘルプが必要なので、私はリサイクルビューを持ち、リサイクルビューの中にボタンがあります。アダプタからフラグメントのメソッドを呼び出す方法

ので、私だけのように呼んで、すでにベースの断片で宣言されたダイアログを開く必要がありますクリックした後にボタン「は、openDialog(DIALOG_CHECK);」

を今私が呼び出すことができますどのように私のアダプタ上のダイアログは、私はすでにメソッドを作ることフラグメントは、アダプタからそれを呼び出すと、エラーの "Java LANG NULLポインタを" 作る

これは私のコードです:

DeliveryFragment delivFrag = new DeliveryFragment(); 
holder.editButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       delivFrag.doEdit(); 
      } 
     }); 

とフラグメントに

public void doEdit(){ 
     openDialog(DIALOG_EDIT_ITEM); 
    } 
+0

インターフェイスをアダプタクラスにして、onClickHandler(View v)メソッドを持つbuttonClickHandlerインターフェイスを呼び出してみましょう。これは、同時に、このインターフェイスを初期化してdoEditメソッドを呼び出すときにフラグメント化されます。 – dex

+0

duplicate of https://stackoverflow.com/questions/24502394/call-a-fragment-method-from-an-adapter –

答えて

1

アダプタクラスにインターフェイスを作成し、アダプタを呼び出す場所からフラグメントにその機能を実装する必要があります。

ここではアイテムのクリック操作のリサイクラービューのサンプルアプリケーションを紹介します。 一度チェックすると便利です。 https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0

3

わかりやすい例です。インターフェイスを使用します。

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> { 

    private static OnItemClickListener mOnItemClickLister; 

    public interface OnItemClickListener { 
    void onItemClicked(View view, int pos); 
    } 

    public void setOnItemClickListener(OnItemClickListener listener) { 

    mOnItemClickLister = listener; 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    Button mBtnTest; 
    Context mContext; 

    //We also create a constructor that accepts the entire item row 
    //and does the view lookups to find each subview. 
    public ViewHolder(Context context, View itemView) { 

     //Stores the itemView in a public final member variable that can be used 
     //to access the context from any ViewHolder Instance 
     super(itemView); 

     mContext = context; 
     mBtnTest = (Button) itemView.findViewById(R.id.message_button); 
     itemView.setOnClickListener(this); 
    } 

    @Override public void onClick(View v) { 
     int position = v.getLayoutDirection(); 
     mOnItemClickLister.onItemClicked(v, position); 
    } 
    } 
} 

     //Fragment Part 

class FragmentTest extends Fragment implements OnItemClickListener { 

    TestAdapter adapter = new TestAdapter(); //you can initialize according to your logic 

    //set the fragment as a listener to adapter 
    this.adapter.setOnItemClickListener(onItemClickListener); 

    public void onItemClicked(View view, int pos) { 
    //do whatever you want here... 
    } 
} 
0

アダプターのコンストラクターにフラグメントインスタンスを送信し、このインスタンスを使用してそのフラグメント内のメソッドを呼び出すことができます。

public MyCartRecycleAdapter(Context context, List<CartData> list, MyFragmentNew myFragmentNew) { 
    this.list = list; 
    this.mContext = (Activity) context; 
    this.myFragmentNew = myFragmentNew; 
} 

、その後

myFragmentNew.MethodName(); 
+0

コンストラクタではなくonBindViewholderからメソッドを呼び出す方法は? –

0

パラメータとしてフラグメントを受け入れるように、アダプタのコンストラクタを更新します。

customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this); 

アダプタクラス:

public CustomAdapter(Context context, int id, HomeFragment fragment) { 
    this.fragment = fragment; 
} 

fragment.doSomething()。

関連する問題