从自定义ListAdapter创建和显示AlertDialog

时间:2011-03-13 21:16:22

标签: android listview alertdialog

我正在实现一个自定义ListAdapter,它使用不同的列表项布局来显示一些项目。从该自定义ListAdapter,我实际上想要在单击特定按钮时显示AlertDialog。我实现了onCreateDialog(int)方法,我试图使用showDialog(int)来显示对话框。但该对话框未显示在活动中。

这是我的自定义listadapter文件


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;
}

}

有人可以给我一些关于如何在点击按钮上显示AlertDialog框的指示。

4 个答案:

答案 0 :(得分:4)

如果您使用上下文来调用showDialog,那么您可能希望在onCreateDialog而不是您的适配器中定义Activity对话框。

答案 1 :(得分:1)

它不起作用,因为onCreateDialog属于Activity类。您不应该在适配器中执行任何操作,而是管理数据。也就是说,将onCreateDialog放入您的活动中并从那里调用它(当然使用showDialog方法:)。

您在其中一个按钮上添加了一个点击监听器;所以你想做的就是向活动发送回调,让它调用showDialog方法。

答案 2 :(得分:0)

您可以使用MaterialDesignLibrary。其中也适用于BaseAdapters和ListAdapters。请在此处查看https://github.com/navasmdc/MaterialDesignLibrary

答案 3 :(得分:0)

在Class CustomAdapter中,您将一个变量mContext和一个ArrayList数据声明为ListView

ArrayList<String> datasource;
Context mContext;

创建一个构造函数:

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

当您从Activity调用CustomAdapter时,“Activity_Main.this”是您需要的上下文

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

现在你有一个Context,使用变量mContext声明替换

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

现在,您可以在所需的CustomAdapter中单击按钮时显示或显示任何对话框。享受您的代码!