我想在onClick
中列出的项目的RecyclerView
事件中打开一个对话框。
这是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
答案 0 :(得分:0)
您的上下文为null,请按照以下步骤在java类中引入上下文
public static Context contextOfApplication; // create variable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contextOfApplication = getApplicationContext();
}
在 Oncreate
之外创建方法public static Context getContextOfApplication(){
return contextOfApplication;
}
现在在你的适配器类中添加上下文
final Dialog dialog = new Dialog("youractivityname".getContextOfApplication());
(OR) @SachinVarma建议
只需使用此 v.getContext()
即可