hamed Salah hamed Salah - 2 months ago 26
Android Question

how to call dialog contain onclicklistener from recycler adapter

I'm trying to calling Dialog by clicking on the Recycle View item, but I receive an error about "Button.setOnClickListener" in my Dialog.

my

RecycleViewAdapter


package com.Mypackage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
* Created by user_adnig on 11/14/15.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

static List<DatabaseModel> dbList;
static Context context;
private static RecyclerViewClickListener itemListener;

RecyclerAdapter(Context context, List<DatabaseModel> dbList, RecyclerViewClickListener itemListener ){
this.dbList = new ArrayList<DatabaseModel>();
this.context = context;
this.dbList = dbList;
this.itemListener = itemListener;

}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.itempost,parent, false);



// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {

holder.name.setText(dbList.get(position).getname());
holder.phone.setText(dbList.get(position).getphone());
holder.adress.setText(dbList.get(position).getadress());
holder.Emaile.setText(dbList.get(position).getTstmp());


}

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

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

public TextView name,phone,adress,Emaile,id;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
name = (TextView) itemLayoutView.findViewById(R.id.edt_name_new);
phone = (TextView)itemLayoutView.findViewById(R.id.edt_phone_new);
adress = (TextView) itemLayoutView.findViewById(R.id.edt_adress_new);
Emaile = (TextView)itemLayoutView.findViewById(R.id.txv_Emaile_new);


itemLayoutView.setOnClickListener(this);

}

@Override
public void onClick(View v) {

showDialog();

}

}


public static void showDialog(){

final Dialog dialog1 = new Dialog(context,R.style.MaterialDialogSheet2);
Button btnedit,btndellet;
btnedit = (Button)dialog1.findViewById(R.id.btnedit);
btndellet = (Button)dialog1.findViewById(R.id.btndellet);
btndellet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});


dialog1.setContentView(R.layout.taskopitondialog);
dialog1.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dialog1.getWindow().setGravity(Gravity.TOP);
dialog1.show();
dialog1.setCanceledOnTouchOutside(true);

}

}


When I run the application receives this error

09-23 13:49:37.081 25068-25068/com.Mypackage E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Mypackage, PID: 25068
java.lang.NullPointerException
at com.Mypackage.RecyclerAdapter.showDialog(RecyclerAdapter.java:99)
at com.Mypackage.RecyclerAdapter$ViewHolder.onClick(RecyclerAdapter.java:86)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)


When I delete "btndellet.setOnClickListener...." And re-run the application, it works fine

Answer

You have to create the dialog first, before you can find your buttons from your layout.

First call setContentView() then call dialog.create(); and then you can use findViewById() for getting references to your buttons.

Comments