0

私はRecyclerViewを実装しました。今度は、RecyclerViewのアイテムがクリックされたときにAlertDialogを表示します。私は多くの方法を試してみましたが、どれも働いていませんここで は私のアダプタクラスで、Android | RecyclerViewのAlertDialogアイテムClick

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.MyViewHolder> { 
private List<Searchresult> resultList; 

public class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView item1, item2, item3; 

    public MyViewHolder(View view) { 
     super(view); 
     item1 = (TextView) view.findViewById(R.id.item1); 
     item2 = (TextView) view.findViewById(R.id.item2); 
     item3 = (TextView) view.findViewById(R.id.item3); 
    } 
} 

public SearchResultAdapter(List<Searchresult> resultList) { 
    this.resultList = resultList; 
} 

@Override 
public MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) { 
    final View itemView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.search_result_row, parent, false); 
    return new MyViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, int position) { 
    Searchresult result = resultList.get(position); 
    holder.item1.setText(result.getItem1()); 
    holder.item2.setText(result.getItem2()); 
    holder.item3.setText(result.getItem3()); 
} 

@Override 
public int getItemCount() { 
    return resultList.size(); 
} 
} 

AlertDialogは、ITEM1を表示ITEM2とRecyclerViewにonClickListenerを実装するために私を助けitem3.Please必要があります。

答えて

0

オープン・アラートダイアログのコードの下に、この

 indexRecyclerView.addOnItemTouchListener(
      new RecyclerItemClickListener(MainActivity.this, indexRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { 

       @Override 
       public void onItemClick(View view, int position) { 
        // show your alert Dailog here 
        } 

       @Override 
       public void onLongItemClick(View view, int position) { 

       } 
      }) 
    ); 
0

使用のようrecyclerviewするリスタをクリックして添付しRecyclerItemClickListener

このような
package com.ncrypted.recyclerviewwithindex; 

import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 


public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { 
    private OnItemClickListener mListener; 

    public interface OnItemClickListener { 
     public void onItemClick(View view, int position); 

     public void onLongItemClick(View view, int position); 
    } 

    GestureDetector mGestureDetector; 

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { 
     mListener = listener; 
     mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { 
      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
       return true; 
      } 

      @Override 
      public void onLongPress(MotionEvent e) { 
       View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
       if (child != null && mListener != null) { 
        mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child)); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { 
     View childView = view.findChildViewUnder(e.getX(), e.getY()); 
     if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { 
      mListener.onItemClick(childView, view.getChildAdapterPosition(childView)); 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { 
    } 


    @Override 
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 
    } 
} 

このレコード生成一つのクラスを試してみてください。

holder.item1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity()); 
       builder1.setMessage("Do you want to remove ?"); 
       builder1.setCancelable(true); 

       builder1.setPositiveButton(
         "Yes", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) 
          {       
         //Do your code... 
           } 
         }); 

       builder1.setNegativeButton(
         "No", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 

       AlertDialog alert11 = builder1.create(); 
       alert11.show(); 
      } 
     }); 
1

問題は、あなたがあなたの活動に

public SearchResultAdapter(List<Searchresult> resultList , Context context) { 
    this.resultList = resultList; 
    this.context = context 
    } 

下記のようにあなたは、その後で

以下

SearchResultAdapter madapter = new SearchResultAdapter (List, this); 
のようなアダプタの使用を開始するところも、アダプタのコンストラクタに contextを渡す必要がここにありますあなたのonBindViewHolder

@Override 
public void onBindViewHolder(final MyViewHolder holder, int position) { 
    Searchresult result = resultList.get(position); 
    holder.item1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //pass the 'context' here 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
      alertDialog.setTitle("Your title"); 
      alertDialog.setMessage("your message "); 
      alertDialog.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 
      alertDialog.setNegativeButton("YES", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        // DO SOMETHING HERE 

       } 
      }); 

      AlertDialog dialog = alertDialog.create(); 
      dialog.show(); 
     } 
    }); 
} 
関連する問題