Android Listview dialog doesn't disapear after choosing an item - android

I've tried to create a list view dialog to display a list of choose. My code is shown below:
LayoutInflater factory=LayoutInflater.from(this);
final View stuckLevelDialogView=factory.inflate(R.layout.report_stuck_dialog, null);
final ListView stuckLevelListViewForDialog=(ListView)stuckLevelDialogView.findViewById(R.id.report_stuck_dialog_listview);
final String[] stuckLevelList=new String[]{"1 - You can move freely","2 - You have to be aware of your movement","3 - You can move slowly","4 - There is a traffic jam","5 - There is a serious traffic jam"};
ArrayAdapter<String> adapterForDialog=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stuckLevelList);
stuckLevelListViewForDialog.setAdapter(adapterForDialog);
final AlertDialog.Builder stuckLevelDialog=new AlertDialog.Builder(this);
stuckLevelDialog.setTitle("What stuck level is this point?");
stuckLevelDialog.setView(stuckLevelDialogView);
stuckLevelDialog.show();
However, when I choose an option, the onItemClick is executed, but the listview dialog doesn't disappear, I have to press back button manually. I've tried to debug the code for a whole day, but it has not been solved yet. Please help me. Thank in advanced!

I think you need to dismiss() the dialog in your onItemClick listener as below:
stuckLevelListViewForDialog.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1,
int p_arg2, long p_arg3) {
stuckLevelDialog.dismiss();
}
});

Use stuckLevelDialog.dismiss; at the end of onItemClick.

You can set setSingleChoiceItems in your alert dialog box with your items list, which will show a list with a radio buttons. If you want to add buttons you can else once user select any items you can dismiss the dialog.
new AlertDialog.Builder(this)
.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// here you can do your functionality and can dismiss dialog as well
dialog.dismiss();
}
})
.show();

Related

I am showing the list of elements in Alert-box by using the setMultiChoiceItems, But when ever i am scrolling the checkbox are dismissed

I have implemented the Alert dialogue with checkbox and item name, I am not using any custom adapter to show the list view.I am able to display the items with checked states successfully but my problem is when ever i scroll the alertbox some of checkbox are unchecked.Could please suggest me how can i resolve this issue.
Please find the code for showing alert box with MultiChoiceItems.
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setPositiveButton(android.R.string.ok, this)
.setOnCancelListener(this)
.setTitle("title")
.setMultiChoiceItems(nameofItems, Checkedstates, listner);
mDialog = builder.create();
mDialog.show();
When you scroll list it update the view using data list so if you want your back check status, you have to update data list as well. Update your data list by using DialogInterface OnMultiChoiceClickListener.
OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Checkedstates[which] = isChecked;
}
}

How to scroll custom list view in Alert Dialog programmatically?

I'm developing an Android app. Currently I've implemented custom list view in Alert Dialog. This is the code so far:
private void setupAlertDialogBuilder(final Context context) {
GiftStoreCountriesAdapter giftStoreCountriesAdapter = new GiftStoreCountriesAdapter(context,
R.layout.gift_store_countries_row,
giftStoreCountriesViewModel.fetchCountriesFromLocalDatabase(),
(HomeActivity) context);
builder = new AlertDialog.Builder(context)
.setIcon(R.drawable.ic_currency_icon)
.setAdapter(giftStoreCountriesAdapter, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
handleCountrySelection(dialog, which);
}
});
builder.create().getListView().setFastScrollEnabled(true);
builder.create().getListView().smoothScrollToPosition(scrollToPosition(context));
}
Everything works fine except one thing. How can I programmatically scroll that custom list view to the some position or index of row as soon as alert dialog is shown to user. I've tried with this:
builder.create().getListView().setFastScrollEnabled(true);
builder.create().getListView().smoothScrollToPosition(scrollToPosition(context));
but it doesn't work. If I put just:
getListView().smoothScrollToPosition(scrollToPosition(context));
without builder.create() I get NullPointerException. This is in class that is custom Alert Dialog and it extends AlertDialog. I need to automatically scroll this list when it's shown to user. I appreciate all your help.

Add modifications to each custom listview item

So right now I have a custom listview adapter that adds another row when the user selects a item. The thing is, each item in the row should have a modification button where they can choose to add whatever modification it is (can choose more than one modification)
This is a food ordering app that when the item is selected, there should be another button in the list labeled "Modify", where a pop-up comes up and allows the user to choose what modification it wants by using checkbox. ("Less salt", "More sauce", etc). Each modification list is the same for each dish. When the user exits the popup and clicks on the same modify button, the checkboxes checked should stay there.
I originally created a Popup class where when the button is selected, there is an intent to jump to that Popup activity, but I couldn't find the relationship between the custom adapter and the Popup activity. I also tried using an AlertDialog to replace the Popup window, but could not find a way to save all the checked items and show which ones were selected before.
Here's my code
modifyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Modification");
final CharSequence[] modify_items = orderClass.getModifyList()
.toArray(new CharSequence[orderClass.getModifyList().size()]);
builder.setMultiChoiceItems(modify_items, null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if(isChecked){
selectedList.add(indexSelected);
selectedItems.set(position, selectedList);
}
else if(selectedList.contains(indexSelected)){
selectedList.remove(Integer.valueOf(indexSelected));
selectedItems.set(position, selectedList);
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
Log.d("dialog", "Showing dialog");
}
});
return view;
}
You need to put data in a holder structure(its kinda a class that holds variables)
then you can show data by index and theres is a relation between the list view index shown and the data index so when is a list view cell clicked you get the position then find it in the structure and post it to another activity or anything else to modify it remember you have to declare a flag to launch the activity else the app will crash i think its called activtyoutofbound index or something like this

Multiple choice alert dialog with custom row layout

I need to create an AlertDialog with multiple choice items but I'm having some trouble trying to set a custom layout file to the internal ListView.
For single choice items I use a constructor that takes a ListAdapter as parameter and this way I can set the proper layout resource for each row:
builder.setSingleChoiceItems(new ArrayAdapter<String>(getActivity(),
R.layout.list_item_single_choice_answer, items), checkedItem,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkedItem = which;
toggleEditTextAnswer(checkedItem == (items.length - 1));
dialog.dismiss();
}
});
The problem is that there's no constructor for setMultiChoiceItems that accepts a ListAdapter as parameter when creating a multiple choice list.
I need to set a custom layout for each row because I use drawable selectors for setting the row background and text color.
Any ideas?
PS. here is the AlertDialog source code for more information.
https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java
Well, I know I should create a custom Dialog but right now I don't have the time to do it ... so this is how I hacked this problem:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the adapter
builder.setAdapter(
new ArrayAdapter<String>(getActivity(),
R.layout.list_item_multiple_choice_answer, items), null)
// Set the action buttons
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.list_item_multiple_choice_answer, items));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view;
checkedItems[position] = !checkedTextView.isChecked();
}
});
listView.setDivider(null);
listView.setDividerHeight(-1);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
setCheckedItems(((AlertDialog) dialog).getListView());
}
});
alertDialog.show();
First I set the adapter with the items and the instead of calling setMultiChoiceItems I get the ListView object from the Dialog and then configure it myself.
I would recommend that you create your own dialog class like this:
Customizing dialog by extending Dialog or AlertDialog
How to create a Custom Dialog box in android?
This way you will have full control over your dialog and you can customize it any way you want.
Also if you still have issues with your list view after that you can customize your list view items completely: (You can only affect the background and text in a small way through xml and selectors without doing your custom implementation)
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Try it out, it may seem hard but when you do it once it will be piece of cake and will do wonders for you in your future development projects.

How do I make a Button to create a new spinner on click

I want to create a form that allows the user to choose as many languages he\she knows (at least one)
I don't want to create an infinite number of Spinner, I want to create a button ("+" button)
that when the user click on it it will create a new spinner that will allow the user to choose another language.
How can I do it?
tnx
button.setOnClickListener(new onClickListener()
{
onClick(View v)
{
Spinner spinner = new Spinner(YourActivity.this);
layout.addView(spinner);
}
});
layout - your parent layout
just implement the Spinner as well, but setVisibility(GONE) and when you click the button change the visibility
you can fill your language data in an arry and set dialogs items with this array.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color);
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}

Categories

Resources