Create and display an AlertDialog from a custom ListAdapter

I am implementing a custom ListAdapter that uses various layouts of list items to show some items. From this custom ListAdapter, I really want to show AlertDialog when a particular button is clicked. I have implemented the onCreateDialog (int) method, and I'm trying to use showDialog (int) to show a dialog. But the dialog does not appear in 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;
}

}

Can someone give me some pointers on how to show the AlertDialog window when a button is clicked.

+3
source share
4 answers

showDialog, , , onCreateDialog Activity, .

+4

, onCreateDialog Activity. , . , onCreateDialog ( showDialog, :).

; , , showDialog.

+1

MaterialDesignLibrary. BaseAdapters ListAdapters. , https://github.com/navasmdc/MaterialDesignLibrary

0

In the CustomAdapter class, you declare the mContext variable and the ArrayList data in the 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 the context you need

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

Now you have the context, use the mContext variable to replace

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

Now you can push or show any dialog box when you click the button in the CustomAdapter. Enjoy your code!

0
source

All Articles