Opening a custom AlertDialog from a ListView button
I am having trouble inflating my custom dialog layout from within a button in a row . It works fine when I start the dialog from a row click , but with a button inside the row click I get . listView
listView
Android.View.InflateException
Here is my method from adapter: getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
final LayoutInflater inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.planets_row, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.zodIcon = (ImageView) convertView.findViewById(R.id.zodiacIcon);
viewHolder.planetName = (TextView) convertView.findViewById(R.id.planetName);
viewHolder.planetDegree = (TextView)convertView.findViewById(R.id.planetDegree);
viewHolder.zodiacName = (TextView)convertView.findViewById(R.id.zodiacName);
viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.planetExplanation);
viewHolder.openDetail = (Button)convertView.findViewById(R.id.planetDetailsBtn);
convertView.setTag(viewHolder);
viewHolder.openDetail.setTag(position);
viewHolder.openDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, viewHolder.planetName.getText().toString(), Toast.LENGTH_SHORT).show();
final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_details, null);
dialog.setView(view);
TextView explan = (TextView)view.findViewById(R.id.explanationDetail);
Button ok = (Button)view.findViewById(R.id.ok);
dialog.setTitle(viewHolder.planetName.getText() +
" " + viewHolder.planetDegree.getText() + " " + viewHolder.zodiacName.getText());
dialog.setIcon(viewHolder.zodIcon.getBackground());
explan.setText(descrip);
dialog.create();
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((DialogInterface) dialog).dismiss();
return;
}
});
dialog.show();
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
planetTitle = getPlanetName(itemsList.get(position).get("planet_id"));
planetDeg = itemsList.get(position).get("degrees")+"\u00B0";
zodiacName = getZodiacName(itemsList.get(position).get("zodiac_id"));
descrip = itemsList.get(position).get("content").toString();
viewHolder.planetName.setText(planetTitle);
viewHolder.planetDegree.setText(" " + planetDeg);
viewHolder.zodiacName.setText(zodiacName);
viewHolder.tvDescription.setText(descrip);
viewHolder.zodIcon.setBackgroundResource(getZodiacDrawable(zodiacName));
return convertView;
}
I have tried other combinations like:
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.dialog_details, null);
dialog.setView(view);
Edit . I added below print logcat
Thank you in advance:)
10-20 11:50:47.431: E/AndroidRuntime(27409): FATAL EXCEPTION: main
10-20 11:50:47.431: E/AndroidRuntime(27409): Process: com.astrolome, PID: 27409
10-20 11:50:47.431: E/AndroidRuntime(27409): android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.createView(LayoutInflater.java:626)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.astrolome.PlanetAdapter$1.onClick(PlanetAdapter.java:71)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.View.performClick(View.java:4633)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.View$PerformClick.run(View.java:19330)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.os.Handler.handleCallback(Handler.java:733)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.os.Looper.loop(Looper.java:157)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.app.ActivityThread.main(ActivityThread.java:5356)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Method.invoke(Method.java:515)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-20 11:50:47.431: E/AndroidRuntime(27409): at dalvik.system.NativeStart.main(Native Method)
10-20 11:50:47.431: E/AndroidRuntime(27409): Caused by: java.lang.reflect.InvocationTargetException
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Constructor.constructNative(Native Method)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
10-20 11:50:47.431: E/AndroidRuntime(27409): ... 19 more
10-20 11:50:47.431: E/AndroidRuntime(27409): Caused by: java.lang.NumberFormatException: Invalid int: "@2131361805"
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.Integer.invalidInt(Integer.java:137)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.Integer.parse(Integer.java:374)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.Integer.parseInt(Integer.java:365)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.content.res.TypedArray.getInt(TypedArray.java:255)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.widget.TextView.<init>(TextView.java:1488)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.widget.TextView.<init>(TextView.java:893)
10-20 11:50:47.431: E/AndroidRuntime(27409): ... 22 more
+3
source to share
2 answers
Try to pass your activity in the adapter constructor and store it in a class member, for example in your context. Then replace this line
final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
from
final AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
+1
source to share