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