Search code examples
androidandroid-fragmentsandroid-alertdialoglistadaptercustom-lists

create alertdialog from a cutom list adapter which is inside a fragment


My app has two fragments, that are swipable as tabs. First fragment displays list of items in list-view using custom list adapter. On clicking an item on the list I want to display a dialog box to input some data. Here is the snapshot of the app enter image description here

Here is the code that is setting the first fragment

private void setFragOne1() {
    String sql = "select admin_email, admin_name, admin_img, admin_contact, admin_status from links";
    cr  = sdb.rawQuery(sql, null);
    try{
        linksList = new ArrayList<>();
        while(cr.moveToNext()) {
            admin_email = cr.getString(0);
            admin_name = cr.getString(1);
            admin_img = cr.getString(2);
            admin_contact = cr.getString(3);
            admin_status = Integer.parseInt(cr.getString(4));
            linksList.add(new f1_row_model(admin_email, admin_name, admin_img, admin_contact, admin_status));
        }
        //last item for add new link
        linksList.add(new f1_row_model("","Click here to add link","","",-1));
        f1adapter = new f1_row_adapter(linksList, getContext());
        lv_all_links.setAdapter(f1adapter);

    }catch (Exception e){
        Log.e("SFO1", e.toString());
    }
}

Here is the code in my adapter that is called on click of the list view

protected void show_subscribe_form() {
    try {
        LayoutInflater inflater = (LayoutInflater) 
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout;
        layout = inflater.inflate(R.layout.subscribe, (ViewGroup) 
findViewById(R.id.root), false);

        ed_subscription_email = (EditText) 
layout.findViewById(R.id.edEmail);
        ed_note = (EditText) layout.findViewById(R.id.edNote);

        //Building dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        builder.setCancelable(false);
        builder.setTitle("Enter the Email Address to Link to");
        builder.setIcon(ContextCompat.getDrawable(mContext, 
R.drawable.linkto));
        builder.setPositiveButton("Subscribe", new 
DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton("Cancel", new 
DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }catch(Exception e){
        Toast.makeText(mContext, "ssf " + e.toString(), 
Toast.LENGTH_SHORT).show();
        Log.e("ssf",e.toString());
    }
}

I'm getting error on this line

layout = inflater.inflate(R.layout.subscribe, (ViewGroup) 
findViewById(R.id.root), false);

and the error is "cannot resolve method findViewById(int)"

What am I doing wrong, here?


Solution

  • Try to change

    layout = inflater.inflate(R.layout.subscribe, (ViewGroup) 
    findViewById(R.id.root), false);
    

    To

    layout = inflater.inflate(R.layout.subscribe,null);