android中自定义适配器中的对话框

时间:2013-09-12 05:15:33

标签: android android-alertdialog custom-adapter

我有一个自定义适配器,有一个复选框和一个textview。我想当用户点击一行时弹出一个警告对话框,用户输入数据。我在活动中使用以下代码并且工作正常:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.floordialog,
            (ViewGroup) findViewById(R.id.floordialog));

    floornum = (EditText) layout.findViewById(R.id.floordialog_floornumber);
    unitnum = (EditText) layout.findViewById(R.id.floordialog_unitnumber);
    Button next = (Button) layout.findViewById(R.id.floordialog_next);
    Button cancel = (Button) layout.findViewById(R.id.floordialog_cancel);


    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("floors");
    builder.setView(layout);
    builder.setCancelable(false);
    builder.create();

    flooralert = builder.create();
    flooralert.show();

    next.setOnClickListener(this);
    cancel.setOnClickListener(this);

但我无法在MyCustomAdapter中使用,因为我不扩展活动,如何解决我的问题? 这是我的CustomAdapter

        public MyadapterForListFloor(Activity a, int textViewResourceId,
        ArrayList<Integer> Floornum) {
    super();
    unitdialog = new UnitDialog();
    this.entries = Floornum;
    this.activity = a;
}

@Override
public int getCount() {

    return entries.size();
}

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    View v = convertView;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.unitview, null);
        holder = new ViewHolder();
        holder.text = (TextView) v.findViewById(R.id.UnitTextview);
        holder.checked = (CheckBox) v.findViewById(R.id.unitCheckbox);

        v.setTag(holder);

        holder.checked.setTag(entries.get(position));
        holder.text.setTag(entries.get(position));

        holder.checked.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewHolder obj = new ViewHolder();
                obj.checked = (CheckBox) v.findViewById(R.id.unitCheckbox);

                getid = (Integer) v.getTag();

                if (isChecked(getid))
                {
                    obj.checked.setChecked(true);
                    unitdialog.DialogForFloor();
                    Urban.selectedRow.add(getid);
                }
                else
                    Log.d("in else onclick", "**");
            }

            private boolean isChecked(int getid) {

                for (int i = 0 ; i< Urban.selectedRow.size() ; i++)
                {
                    if (Urban.selectedRow.get(i)== getid)
                        return false;
                }
                return true;
            }

        });
        holder.text.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                holder.checked.performClick();
            }
        });
    }
    else 
    {
        ((ViewHolder) v.getTag()).checked.setTag(entries.get(position));
        holder = (ViewHolder) v.getTag();

        getid = (Integer) holder.checked.getTag();

        Log.d("geti is", String.valueOf(getid));

        if (checkBoxRefresh(getid))
            holder.checked.setChecked(true);


        else
            holder.checked.setChecked(false);

    }

    final String str = String.valueOf(entries.get(position));
    if (str != null) {

        holder.text.setText(str);

    }

    return v;
}

private boolean checkBoxRefresh(int FloorNum) {
    Log.d("urban.selectedrow.size is", String.valueOf(Urban.selectedRow.size()));
    for (int i = 0 ; i < Urban.selectedRow.size() ; i++)
    {
        if (Urban.selectedRow.get(i) == FloorNum)
            return true;
    }
    return false;

}

static class ViewHolder {
    TextView text;
    CheckBox checked;

}

}

3 个答案:

答案 0 :(得分:2)

试试这个:

 LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

您已将上下文传递给MyCustomAdapter

答案 1 :(得分:2)

在Class CustomAdapter中,您需要声明一个类级变量mContext

Context mContext;

创建一个构造函数:

public AdapterAudio(Context mContext) {
    super();
    this.mContext = mContext;
}

当你从Activity调用CustomAdapter时,&#34; Activity_Main.this&#34;是你需要的背景

CustomAdapter adapter = new CustomAdapter(Activity_Main.this);

不要通过getApplicationContext(),我们需要将MainActivity.this传递给构造函数,因为AlertDialog class需要此代码才能显示警告。

如果您通过getApplicationContext()getContext(),则会收到以下错误:

W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

现在,传递mContext以显示警告:

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle("floors");
    builder.setView(layout);
    builder.setCancelable(false);
    builder.create();

    flooralert = builder.create();
    flooralert.show();

答案 2 :(得分:0)

只需将必要的Context传递给活动中的adapter,然后在您的dailog中使用该上下文。

下面@

AlertDialog.Builder builder = new AlertDialog.Builder(here will be your context which is from activity);

正如您在评论中提到的那样,您必须自定义对话框,并且您必须执行findviewById。

你可以这样做@(这只是一个例子)

<强>更新

AlertDialog.Builder builder = new AlertDialog.Builder(your_activity_context);
View view = your_activity_context.getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(view);

 Button button = (Button) view.findViewById(R.id.dialog_ok);