0

我有一个自定义适配器,它有一个复选框和一个文本视图。我希望​​当用户单击一行时弹出一个警报对话框并用户输入数据。我在活动中使用以下代码并且工作正常:

    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;

}

}

4

3 回答 3

3

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

Context mContext;

创建一个构造函数:

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

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

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();
于 2016-02-27T10:11:03.690 回答
2

尝试这个:

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

您已经将 Context 传递给MyCustomAdapter.

于 2013-09-12T05:35:38.287 回答
0

只需将必要的传递Context给您的adapterfrom 活动并在您的 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);
于 2013-09-12T05:26:28.667 回答