In my App I have created an Alertdialog with a Title, listview item and Cancel button. I have set Item click on each of the item of the list. So if user click on item, it will generate another Alertdialog with a title, listview and cancel. The cancel button for first Alerdialog is working properly. But the cancel button of the second Alerdialog is nnot working. I am no finding out what would be the reason behind it. I have implemented the similar code. Here is my code.
public class AlertDialogFragment extends DialogFragment{
private ListView listView1;
private ListView listView2;
private Button cancelButton1;
private Button cancelButton2;
private String[] companyName;
private String[] actionName;
private ArrayAdapter<String> adapter;
public AlertDialogFragment(){
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setCancelable(true);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_alertlist_contact, container, false);
//Set Title Dialog
getDialog().setTitle("Contact");
//Button,ListView1 Initialization
listView1=(ListView) rootView.findViewById(R.id.listView1);
cancelButton1=(Button) rootView.findViewById(R.id.cancel_button1);
// Defined Array values to show in ListView
companyName = getResources().getStringArray(R.array.company_name);
//Create and set Adepter TO ListView1
adapter=new ArrayAdapter<String>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,companyName);
listView1.setAdapter(adapter);
cancelButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
dismiss();
showDialog2();
}
if(itemPosition == 1)
{
dismiss();
showDialog2();
}
if(itemPosition == 2)
{
....
}
}
});
return rootView;
}
private void showDialog2(){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.second_alertlist_contact, null);
dialogBuilder.setView(dialogView);
listView2 = (ListView) dialogView.findViewById(R.id.listView2);
cancelButton2=(Button) dialogView.findViewById(R.id.cancel_button2);
// Defined Array values to show in ListView
actionName = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, actionName);
listView2.setAdapter(adapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
dismiss();
System.out.println("Hello");;
}
...
}
});
cancelButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
dialogBuilder.show();
}
}
Here is my XML code for first AlertDialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Contact"
android:gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:background="#283593"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center"
>
</ListView>
<Button
android:id="@+id/cancel_button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:text="Cancel"
android:textColor="#FFFFFF"
android:background="#283593"/>
</LinearLayout>
And XML code for Second Alert
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="What do you want to do"
android:gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:background="#283593"/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center"
>
</ListView>
<Button
android:id="@+id/cancel_button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:text="Cancel"
android:textColor="#FFFFFF"
android:background="#283593"/>
I have to post this as an answer and not a comment because of my reputation. However, does the AlertDialog contain the list view?
So in your AlertDialog, you will have
Title
LISTVIEW
Cancel
I will then edit this post a solution if that is the case. Your code is very quirky... so I think the best bet is to start from scratch.
EDITED WITH CODE BELOW
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.alert_listview, null);
//Set the layout inside of the builder
builder.setView(builderView);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) builderView.findViewById(R.id.alertTitle);
Button alertButton = (Button) builderView.findViewById(R.id.alertButton);
ListView alertListView = (ListView) builderView.findViewById(R.id.listView);
alertTitle.setText("YOU CAN SET THIS TO WHATEVER");
//Click the alert button from within the alert will dismiss the dialog box
alertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});
alert_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
android:text="Title"
android:id="@+id/alertTitle"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/alertTitle">
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="0dip"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:id="@+id/alertButton"
android:text="BUTTON"/>
</LinearLayout>
</RelativeLayout>
HERE IT IS WITH A LIST INSIDE THE DIALOG
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View builderView = getLayoutInflater().inflate(R.layout.alert_listview, null);
//Set the layout inside of the builder
builder.setView(builderView);
//Show the dislog
final AlertDialog alert = builder.show();
List<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
arrayList.add("6");
arrayList.add("7");
arrayList.add("8");
arrayList.add("9");
arrayList.add("10");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, arrayList);
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) builderView.findViewById(R.id.alertTitle);
Button alertButton = (Button) builderView.findViewById(R.id.alertButton);
ListView alertListView = (ListView) builderView.findViewById(R.id.listView);
alertListView.setAdapter(arrayAdapter);
alertTitle.setText("YOU CAN SET THIS TO WHATEVER");
//Click the alert button from within the alert will dismiss the dialog box
alertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});