我想在RecyclerView
中列出的项目的onClick
事件中打开一个对话框。
这是Activity
代码:
public class EventPage extends AppCompatActivity {
EventAdapter adapter;
private RecyclerView recyclerView;
ArrayList<String> ticketName=new ArrayList<String>();
ArrayList<String>ticketPrice=new ArrayList<String>();
ArrayList<String>ticketCode=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eventfinished);
recyclerView = (RecyclerView) findViewById(R.id.rvUserProfile);
adapter = new EventAdapter(ticketName, ticketPrice,getApplicationContext(),recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter.notifyDataSetChanged();
这是Adapter
代码:
public class EventAdapter extends RecyclerView.Adapter<info.androidhive.uplus.EventAdapter.MyViewHolder> {
private ArrayList<String> mDataset=new ArrayList<String>();
private ArrayList<String> mPrice=new ArrayList<String>();
Context ctx;
RecyclerView rec;
Uri uri;
public EventAdapter(ArrayList<String> mDataset,ArrayList<String> mPrice ,Context ctx,RecyclerView rec){
this.mDataset = mDataset;
this.mPrice = mPrice;
this.ctx = ctx;
this.rec =rec;
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
// public CardView mCardView;
public TextView mTextView, mTicketPrice;
public RelativeLayout relativeLayout;// mCardView;
public MyViewHolder(View v, final Context context,final ArrayList<String>eventName, final ArrayList<String>eventPrice,final RecyclerView rcc){
super(v);
mTextView = (TextView) v.findViewById(R.id.ticketName1);
mTicketPrice = (TextView) v.findViewById(R.id.ticketamount);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int p=rcc.indexOfChild(v);
try
{
final Dialog dialog = new Dialog(context);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bookticket);
dialog.show();
Toast.makeText(context,"Clicked"+eventName.get(p),Toast.LENGTH_LONG).show();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
}
}
@Override
public info.androidhive.uplus.EventAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.eventitem, parent, false);
info.androidhive.uplus.EventAdapter.MyViewHolder vh = new info.androidhive.uplus.EventAdapter.MyViewHolder(v, this.ctx, this.mDataset, this.mPrice, this.rec);
return vh;
}
@Override
public void onBindViewHolder(final info.androidhive.uplus.EventAdapter.MyViewHolder holder, final int position){
holder.mTextView.setText(mDataset.get(position));
holder.mTicketPrice.setText(currencyConverter(mPrice.get(position)));
}
这是我在列表中单击元素时遇到的错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
您的上下文是空的,请按以下步骤带来Java类中的上下文
public static Context contextOfApplication; // create variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contextOfApplication = getApplicationContext();
}
在外部创建方法 ongreate
public static Context getContextOfApplication(){
return contextOfApplication;
}
现在在适配器类中添加上下文
final Dialog dialog = new Dialog("youractivityname".getContextOfApplication());
(或( @sachinvarma建议
只需使用此 v.getContext((