Not dismiss dialog when using multiple choice AlertDialog - android

I'm using setAdapter() on AlertDialog.Builder to supply my own custom ListAdapter for my list. But after my DialogInterface.OnClickListener() is called the dialog is automatically dismissed.
Below there is the Android source code in AlertController.createListView(...) that causes my dialog to dismiss:
if (mOnClickListener != null) {
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
mOnClickListener.onClick(dialog.mDialogInterface, position);
if (!mIsSingleChoice) {
dialog.mDialogInterface.dismiss();
}
}
});
}
Currently I'm using AlertDialog.Builder.setSingleChoiceItems(..) instead of setAdapter(..). Is there any better way to use a custom ListAdapter and allow multiple choice in the dialog? I find it misleading to use setSingleChoiceItems for a multiple choice list.

#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
m_adapter = new MyAdapter();
builder.setAdapter(m_adapter, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog alertDialog = builder.create();
// override default behavior of dismissing dialog on clicking list item
final ListView listView = alertDialog.getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
return alertDialog;
}

You can dismiss AlertDailog in either way of using Handler class. See following code,
if (mOnClickListener != null)
{
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
mOnClickListener.onClick(dialog.mDialogInterface, position);
if (!mIsSingleChoice)
{
//dialog.mDialogInterface.dismiss();
// call handler class as follows,
handler.sendMessage(handler.obtainMessage());
}
}
});
}
private Handler handler = new Handler()
{
#Override
public void handleMessage ( Message message )
{
dialog.mDialogInterface.dismiss();
}
};

Related

Remove item list from DB MSSQL using ListView.setOnItemLongClickListener Android

I have created a database which is displayed in a ListView using an SimpleAdapter. Now, I want to show a layout which contain (edit & delete ), so when user longClick on an item of the list it will get a dialog to choose if its want to delete or to edit on this item. Now, I'm focusing on delete statement and
I have tried with this code, but it does not work for me, the program always force close when I use this code.
Thanks
#Override
protected void onCreate(Bundle savedInstanceStates){
super.onCreate(savedInstanceStates);
setContentView(R.layout.activity_user_lstview);
userDataListView = (ListView) findViewById(R.id.ListViewUser);
List<Map<String, String>> data = new ArrayList<Map<String, String>>(
String[] from = {"tvId", "tvUsr", "tvPass", "tvDept","tvAuth"};
int[] to = {R.id.tvId, R.id.tvUsr, R.id.tvPass, R.id.tvDept, R.id.tvAuth};
adapter = new SimpleAdapter(UserListView.this, data, R.layout.userlisttemplate, from, to);
userDataListView.setAdapter(adapter);
userDataListView.setLongClickable(true);
userDataListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"Clicked item : " +adapter.getItem(position), Toast.LENGTH_LONG).show();
}
});
userDataListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
final int which_item = position;
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(UserListView.this);
dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
dialogBuilder.setTitle("Confirmation");
dialogBuilder.setMessage("Choose an option edit or delete data ?");
dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
// Edit
­­
dialogInterface.dismiss();
}
});
dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
// Delete
data.remove(which_item);
adapter.notifyDataSetChanged();
Toast.makeText(UserListView.this, "Data has been removed", Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
dialogBuilder.create().show();
return true;
}
});
}

Activate Phone call on ItemClick

In my app I am trying to activate phonecall on a Item click for a list item click. But this code is not working at all. After running this code whenever I click on phone call the app is crashing . I have attached my crash report here. As I am very new in android developing. I am not sure how to get rid of thi serror.
My code is-
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;
protected FragmentActivity mActivity;
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)
{
dismiss();
showDialog2();
}
.....
}
});
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)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
//builder.setMessage("Are you sure you want to log out?");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if(itemPosition == 1)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 2)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 3)
{
dismiss();
System.out.println("Hello");;
}
}
});
dialogBuilder.show();
}
}
07-10 15:40:49.134 12970-12970/com.testgrid E/InputEventReceiver: Exception dispatching input event.
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at
android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at
android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at
com.testgrid.AlertDialogFragment$3.onItemClick(AlertDialogFragment.java:155)
at
android.widget.AdapterView.performItemClick(AdapterView.java:310)
You need some permissions forr making calls, put this in your manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
maybe this tutorial helps https://www.mkyong.com/android/how-to-make-a-phone-call-in-android/
Use below code to make dialog and call button :
Put context youractivityname.this or if you are in a fragment getActivity().
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle("Title")
.setMessage("some message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// call specific number
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do anything
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
source

Android - spinner position value

Im using a Spinner in an AlertDialog as a Filter. When I reopen the Dialog after selecting an item, I want to select the chosen item from before by default. But some kind of magic is happening and I cant understand why the spinner is selecting the wrong item. The item list for the spinner is a String array with numbers from 4 to 42. {"4","6",.."42"}
When I select the first item "4" the position should be 0 and when I reopen the Dialog the exact item should be displayed. But instead it displays the item "10" on position 4.
Code Dialogfragment:
public class FittingSelectionDialogFragment extends DialogFragment {
private String dTitle;
private String[] list;
private int position;
private String filterItem;
private List<String> chosenFilterItems;
public interface FittingSelectionDialogListener {
public void onChosenDialogItem(String filterStrings,int menuPosition);
}
FittingSelectionDialogListener mListener;
public Dialog onCreateDialog(Bundle savedInstanceState){
final Spinner spinner2 = new Spinner(getActivity());
final TextView label = new TextView(getActivity());
final TextView label2 = new TextView(getActivity());
LinearLayout layout = new LinearLayout(getActivity());
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getContext(), R.layout.support_simple_spinner_dropdown_item, list);
spinner2.setAdapter(adapter2);
if(chosenFilterItems.get(position).equals("")) {
spinner2.setSelection(0);
}
else{
spinner2.setSelection(Integer.valueOf(chosenFilterItems.get(position)));
}
label.setText("Rohr AD:");
label.setTextColor(getResources().getColor(R.color.colorParkerBlack2));
label.setTextSize(16);
label.setPadding(60,0,40,0);
label2.setText("mm");
label2.setTextSize(16);
label2.setTextColor(getResources().getColor(R.color.colorParkerBlack2));
dialog.setTitle(getdTitle());
layout.addView(label);
layout.addView(spinner2);
layout.addView(label2);
dialog.setView(layout);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int spinner2Position, long id) {
filterItem = list[spinner2Position];
chosenFilterItems.set(position,String.valueOf(spinner2Position));
Toast.makeText(getContext(),chosenFilterItems.get(position),Toast.LENGTH_SHORT).show(); //correct value
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
dialog.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onChosenDialogItem(filterItem,position);
Toast.makeText(getContext(),chosenFilterItems.get(position),Toast.LENGTH_SHORT).show();//wrong value
dismiss();
}
});
dialog.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
filterItem = "";
chosenFilterItems.set(position,"");
mListener.onChosenDialogItem(filterItem, position);
}
});
return dialog.create();
}
}
I made two Toasts inside the Listeners. The crazy thing is that in the OnItemSelectedListener the correct position value is displayed. In the PositiveButton OnClickListener is displaying the wrong value. Have you any idea?
I made a workaround by myself. I did not found the mistake by debugging. So I dont work with the position. I use the value to reselect the chosen Item. Not the best way, but works for this small itemlist.
Code:
if(chosenFilterItems.get(position).equals("")) {
spinner2.setSelection(0);
}
else{
for(int i=0; i<list.length;i++){
if(list[i].equals(chosenFilterItems.get(position))){
spinner2.setSelection(i);
}
}
}
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int spinner2Position, long id) {
filterItem = list[spinner2Position];
}
dialog.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
chosenFilterItems.set(position,filterItem);
mListener.onChosenDialogItem(filterItem,position);
dismiss();
}
});
Use Handler and add some delay
int delay = 1000;
runOnUiThread (new Runnable () {
#Override
public void run () {
if(chosenFilterItems.get(position).equals("")) {
spinner2.setSelection(0);
} else {
Integer.valueOf(chosenFilterItems.get(position)));
}
}
}, delay);

Android GridView item click Alert Dialog

I have an AlertDialog with the GridView to choose a color, now I want to close the AlertDialog when click item . How can I do?
private void Alert_Colori() {
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.color_picker, null);
GridView gridViewColors = (GridView) view.findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(this));
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
}
});
customDialog.setView(view);
customDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
customDialog.setView(view);
customDialog.show();
}
First Add
final Dialog dialog = customDialog .create();
after the line
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
And then add this in your code
dialog.dismiss();
to
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
}
ie, your code for the ItemClickListener will be as follows
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
}
});
Also, please remember to change customDialog.show(); to dialog.show()
Do this - customDialog.dismiss();
And try changing
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
to
final AlertDialog customDialog = new AlertDialog.Builder(this);
in your original code like this -
private void Alert_Colori() {
final AlertDialog customDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.color_picker, null);
GridView gridViewColors = (GridView) view.findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(this));
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
customDialog.dismiss();
}
});
customDialog.setView(view);
customDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
customDialog.setView(view);
customDialog.show();
}
As describe here: https://stackoverflow.com/a/23372134 Get the instance of AlertDialog using create

DialogFragment with extra space below ListView

As you can see, below the bottom list element in my ListView, there is excess space I can't seem to be rid of. I've tried Relative and Linearlayout, both look like this. Here's the code:
public class ChooseDialog extends DialogFragment implements
DialogInterface.OnClickListener {
String URLhome;
String Title;
String type;
/* public static ChooseDialog newInstance() {
ChooseDialog dialog = new ChooseDialog();
Log.v("a", "shit runs");
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
return dialog;
}*/
public ChooseDialog(String type) {
this.type = type;
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(type);
builder.setNegativeButton("Cancel", this);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogLayout);
final String[] items = {"Red", "Green", "Blue" };
builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.v("touched: ", items[which].toString());
}}
);
return builder.create();
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
And the code that launches the dialog:
public OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
showNationalityDialog();
}
};
private void showNationalityDialog() {
FragmentManager fm = getSupportFragmentManager();
ChooseDialog nationalityDialog = new ChooseDialog("Nationality");
nationalityDialog.show(fm, "fragment_edit_name");
}
I know this question never drew much attention, but I finally solved the problem.
By using the listview that I created in XML rather than setting the builder's adapter, I managed to get rid of all the excess space.
Here's what the new code looks like:
switch (editText.getId()) {
case (0) :
ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist,
activity.getResources().getStringArray(R.array.ageArray)));
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
editText.setText(activity.getResources().getStringArray(R.array.ageArray)[arg2]);
dismiss();
}
});
builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) :
new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);
builder.setNegativeButton("Cancel", this);
builder.setView(dialogLayout);
return builder.create();
If you are setting a custom view on the alert dialog (via setView()) that ONLY has a ListView then you don't need to use a custom view. The builder will automatically add a ListView into the view if set adapter is called.
The extra space at the end of the list view is probably your custom view with no content.
For example:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AccountChooserListAdapter adapter = new AccountChooserListAdapter(getActivity(), R.layout.choose_account_list_item,
accountMetadataFactory.getAccountsAsList());
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setTitle(getActivity().getString(R.string.title_add_account))
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onAddAccount(which);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
}

Categories

Resources