Avatar billede qiTsuk Nybegynder
09. marts 2016 - 11:30 Der er 1 løsning

findViewById fra Custom AlertDialog

Jeg arbejder med en custom version af AlertDialog klassen, hvor jeg sætter et custom view. Mit problem er så at jeg ikke kan få fat i de Views jeg opretter i mit Custom View. Jeg har helt specifikt 2 EditText fields som jeg ikke kan få adgang til.

Jeg bruger denne klasse;


import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


public class QustomDialogBuilder extends AlertDialog.Builder{

    /** The custom_body layout */
    private View mDialogView;

    /** optional dialog title layout */
    private TextView mTitle;
    /** optional alert dialog image */
    private ImageView mIcon;
    /** optional message displayed below title if title exists*/
    private TextView mMessage;
    /** The colored holo divider. You can set its color with the setDividerColor method */
    private View mDivider;

    public QustomDialogBuilder(Context context) {
        super(context);

        mDialogView = View.inflate(context, R.layout.qustom_dialog_layout, null);
        setView(mDialogView);

        mTitle = (TextView) mDialogView.findViewById(R.id.alertTitle);
        mMessage = (TextView) mDialogView.findViewById(R.id.message);
        mIcon = (ImageView) mDialogView.findViewById(R.id.icon);
        mDivider = mDialogView.findViewById(R.id.titleDivider);
    }

    /**
    * Use this method to color the divider between the title and content.
    * Will not display if no title is set.
    *
    * @param colorString for passing "#ffffff"
    */
    public QustomDialogBuilder setDividerColor(String colorString) {
        mDivider.setBackgroundColor(Color.parseColor(colorString));
        return this;
    }

    @Override
    public QustomDialogBuilder setTitle(CharSequence text) {
        mTitle.setText(text);
        return this;
    }

    public QustomDialogBuilder setTitleColor(String colorString) {
        mTitle.setTextColor(Color.parseColor(colorString));
        return this;
    }

    @Override
    public QustomDialogBuilder setMessage(int textResId) {
        mMessage.setText(textResId);
        return this;
    }

    @Override
    public QustomDialogBuilder setMessage(CharSequence text) {
        mMessage.setText(text);
        return this;
    }

    @Override
    public QustomDialogBuilder setIcon(int drawableResId) {
        mIcon.setImageResource(drawableResId);
        return this;
    }

    @Override
    public QustomDialogBuilder setIcon(Drawable icon) {
        mIcon.setImageDrawable(icon);
        return this;
    }

    /**
    * This allows you to specify a custom layout for the area below the title divider bar
    * in the dialog. As an example you can look at example_ip_address_layout.xml and how
    * I added it in TestDialogActivity.java
    *
    * @param resId  of the layout you would like to add
    * @param context
    */
    public QustomDialogBuilder setCustomView(int resId, Context context) {
        View customView = View.inflate(context, resId, null);
        ((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
        return this;
    }

    @Override
    public AlertDialog show() {
        if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
        return super.show();
    }

}



oG så i min main har jeg noget ala;


QustomDialogBuilder qBuilder = new  QustomDialogBuilder(MainActivity.this)
.setTitle("Overskrift")
.setTitleColor("#92161A")
.setDividerColor("#92161A")
.setCustomView(R.layout.my_custom_layout, MainActivity.this)
.show();


Alt dette virker fint, men når jeg så prøver at få fat i mine EditTexts inde i mit custom layout;

EditText usernameEditText = (EditText)findViewById(R.id.username);
[/]
Får jeg en nullpointer.
Jeg har prøvet at lave en
[i]
qBuilder.findViewById(...)

Men metoden er ikke defineret. Skal jeg selv definere den i min QustomDialogBuilder, eller er der en anden måde at gøre det på?

Pft.
Avatar billede qiTsuk Nybegynder
09. marts 2016 - 11:38 #1
LoL, det er rubber duck debugging så det vil noget det her! :P Jeg fandt selv løsningen, ved at tage mit eget forslag til mig og lave min egen


public View findViewById(int resId) {
    return mDialogView.findViewById(int resId);
}


i QustomDialogBuilder klassen! ^_^
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester