问题
I have one custom adapter that have one checkbox and one textview.I want when user clicked on one row one alert dialog pop up and user enter data.I use following code in activity and worked fine:
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);
but i cant use in MyCustomAdapter because i Dont extend activity, how can solve my problem? and this is my 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;
}
}
回答1:
In Class CustomAdapter you need to declare a class level variable mContext
Context mContext;
Create a constructor:
public AdapterAudio(Context mContext) {
super();
this.mContext = mContext;
}
When you call CustomAdapter from Activity, "Activity_Main.this" is Context you need
CustomAdapter adapter = new CustomAdapter(Activity_Main.this);
Don't pass getApplicationContext()
, we need to pass MainActivity.this
to the constructor as AlertDialog class
need this to show alert.
If you pass getApplicationContext()
or getContext()
then you will get the following error:
W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Now, pass the mContext to show alert:
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("floors");
builder.setView(layout);
builder.setCancelable(false);
builder.create();
flooralert = builder.create();
flooralert.show();
回答2:
Try this:
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
You are already passing your Context to MyCustomAdapter
.
回答3:
Simply pass necessary Context
to your adapter
from activity and use that context in your dailog.
Here@
AlertDialog.Builder builder = new AlertDialog.Builder(here will be your context which is from activity);
As you mention in your comment that you have to custom dialog and you have to do findviewById.
you can do like this@ (It just an example)
Updated
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);
来源:https://stackoverflow.com/questions/18755847/dialog-in-custom-adapter-in-android