Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and Showing an AlertDialog from a custom ListAdapter

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.

like image 815
chaitanya Avatar asked Dec 07 '25 04:12

chaitanya


2 Answers

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.

like image 192
Matthew Willis Avatar answered Dec 08 '25 16:12

Matthew Willis


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.

like image 45
Cristian Avatar answered Dec 08 '25 17:12

Cristian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!