3

I am implementing a custom ListAdapter which is using different list item layouts to show some items. From that custom ListAdapter, I actually want to show an AlertDialog when a particular Button is clicked. I implemented the onCreateDialog(int) method and I am trying to use showDialog(int) to show the dialog. But the dialog does not show up in the Activity.

Here is my custom listadapter file


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AddProblemsLayoutAdapter extends BaseAdapter {

    private Activity mContext;
    private static final int TYPE_TITLE = 0;
    private static final int TYPE_TAG = 1;
    private static final int TYPE_SOLUTION = 2;
    private static final int LAYOUT_MAX_COUNT = TYPE_SOLUTION + 1;
    private static final int ADD_TAG_DIALOG = 3378;
    private static int ITEM_COUNT = 4;
    private static Button addSolution = null, addTag = null;
private Activity mContext;
private static final int TYPE_TITLE = 0;
private static final int TYPE_TAG = 1;
private static final int TYPE_SOLUTION = 2;
private static final int LAYOUT_MAX_COUNT = TYPE_SOLUTION + 1;
private static final int ADD_TAG_DIALOG = 3378;
private static int ITEM_COUNT = 4;
private static Button addSolution = null, addTag = null;


public AddProblemsLayoutAdapter(Activity aContext) {

    mContext = aContext;
}

@Override
public int getViewTypeCount() {
    return LAYOUT_MAX_COUNT;
}

protected Dialog onCreateDialog(int id) {
    AlertDialog dialog = null;
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    switch (id) {
    case ADD_TAG_DIALOG:
        builder.setMessage("Are you sure you want to exit?").setCancelable(
                false).setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                }).setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        dialog = builder.create();
        break;
    default:
        dialog = null;
    }
    dialog.setOwnerActivity(mContext);
    return dialog;
}

@Override
public int getItemViewType(int position) {
    if (position < 2)
        return TYPE_TITLE;
    else
        return position > 2 ? TYPE_SOLUTION : TYPE_TAG;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);
    if (convertView == null) {
        LayoutInflater inflater = mContext.getLayoutInflater();
        switch (type) {
        case TYPE_TITLE:
            convertView = inflater.inflate(R.layout.title_row, null);
            break;

        case TYPE_TAG:
            convertView = inflater.inflate(R.layout.tag_row, null);
            break;
        case TYPE_SOLUTION:
            convertView = inflater.inflate(R.layout.solution_row, null);
            break;
        }
    }
    if (position == 0) {
        TextView titleText = (TextView) convertView
                .findViewById(R.id.titleText);
        titleText.setText(R.string.title_string);
    } else if (position == 1) {
        TextView titleText = (TextView) convertView
                .findViewById(R.id.titleText);
        titleText.setText(R.string.description_string);
    } else if (position == 2) {
        addTag = (Button) convertView.findViewById(R.id.addProblemTag);
        addTag.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mContext.showDialog(ADD_TAG_DIALOG);
                Toast.makeText(mContext, "Add Tags", Toast.LENGTH_LONG)
                        .show();
            }
        });

    } else if (position == 3) {
        addSolution = (Button) convertView.findViewById(R.id.addSolution);
        addSolution.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ITEM_COUNT++;
                notifyDataSetChanged();
            }
        });
    }
    return convertView;
}

@Override
public int getCount() {
    return ITEM_COUNT;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

}

Could anyone give me some pointers as to how to show the AlertDialog box on the button click.

4 Answers 4

4

If you're using the context to call showDialog then you probably want to define the onCreateDialog dialog in your Activity and not your adapter.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. I just had a small question. The documentation states that you can define the creation of the Dialogs in a function other than onCreateDialog and then a set an owner activity. Can this be achieved by having a custom function in my adapter(though I now know it is not recommended :) ) and then setting the Activity where to display the Dialog in setOwnerActivity(Activity)
Yes, that's possible. You just won't get any help from the framework in terms of calling your custom function, you'll have to do it yourself.
1

It does not work because onCreateDialog belongs to the Activity class. You shouldn't do anything in the adapter but manage data. That said, put the onCreateDialog inside your activity and call it from there (using the showDialog method, of course :).

You added a click listener to one of the buttons; so what you want to do there is sending a callback to the activities for it to invoke the showDialog method.

2 Comments

Thanks that works. I just had a small question. The documentation states that you can define the creation of the Dialogs in a function other than onCreateDialog and then a set an owner activity. Can this be achieved by having a custom function in my adapter(though I now know it is not recommended :) ) and then setting the Activity where to display the Dialog in setOwnerActivity(Activity)
Well, actually you can create dialogs whereever you want. onCreateDialog is useful since it will take care of configuration changes for you... but you can use a dialog whereever you want... just create it and invoke its show method.
0

You can use the MaterialDesignLibrary. Which also will work from the BaseAdapters and ListAdapters. please check it here https://github.com/navasmdc/MaterialDesignLibrary

Comments

0

In Class CustomAdapter you declare a variable mContext and a ArrayList data to ListView

ArrayList<String> datasource;
Context mContext;

Create a constructor:

 public AdapterAudio(Context mContext, ArrayList<String> data) {
    super();
    this.datasoure = data;
    this.mContext = mContext;
}

When you call CustomAdapter from Activity, "Activity_Main.this" is Context you need

CustomAdapter adapter = new CustomAdapter(Activity_Main.this, listAudio_hienthi10);

Now you have a Context, use variable mContext is declared to replace

"getContext()", "v.getContext()"

Now you can Toast or show any dialog when click Button in CustomAdapter you want. Enjoy your code!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.