Can't get AlertDialog to dismiss when an item is selected - android

Hey guys, I am having trouble getting my alert dialog to dismiss when an item is selected. I used an AlertDialog builder to display it and I believe that this is my problem. I cannot call dismiss because their is not builder.dismiss() method. Is there a simple way to fix this problem? Here is my code. Thanks.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.numplayersdialog,
(ViewGroup) findViewById(R.id.layout_root));
final Spinner spinner = (Spinner) layout.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num_players_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
builder.setTitle("Select Number of Players");
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int item = spinner.getSelectedItemPosition();
commandWriter(item);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
builder.setView(layout);
builder.show();

builder.show() returns an AlertDialog object. You can use this to close the dialog.

Related

How to close a dialog after the user selects an item from a spinner?

I have a dialog with a spinner. Currently, the dialog and the spinners works fine. However, the spinner doesn't close after I selected an item. I need it to be close and return to the activity after an item is selected. Thanks.
Here's the code for my dialog.
String[] s = {"A", "B", "C", "D", "E", "F" };
final ArrayAdapter<String> adp = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, s);
final Spinner sp = new Spinner(getActivity());
//sp.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
Log.d("selectedItem:", selectedItem);
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(sp);
builder.create().show();
There is no method on Spinner to close it,when spinner item is selected, and that will close your spinner, add to your setOnItemSelectedListener
sp.setSelection(int position)
and then just change your AlertDialog constructor a bit, put alert.dismiss(); to dismiss the AlertDialog after user selects an item in your spinner
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(sp);
final AlertDialog alert = builder.create();
alert.show();
Try this
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final Spinner sp = new Spinner(getActivity());
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
Log.d("selectedItem:", selectedItem);
builder.dismiss();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
builder.setView(sp);
builder.create().show();

I want to get the selected value from List view in alert dialog

I have ArrayList of object (citiesInSpinner) each object have two value(Id, Name)
I have already get it in alert dialog
I use this function to alert dialog:
public void test()
{
FillSpinner();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchFligtsActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Select City");
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,citiesInSpinner);
lv.setAdapter(adapter);
/* alertDialog.setItems(citiesInSpinner, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
/////
}
});*/
alertDialog.show();
/**/
}
now I want to make something to get the ((ID)) of item (I mean the Id of City)
I tried to do that but I failed...
any help Please !!
and thank you
Simply use
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
City city = citiesInSpinner.get(position)
//get your id -> city.Id
}
});

Android - spinner within dialog won't populate

I'm trying to add a two spinners inside a dialog (popup). The problem I'm having is populating the spinners. I get no error I can see, and basically the same code works if It's in a tab-fragment, and not the dialog.
This is the code that does not popluate the spinners inside the dialog.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
League league;
league = ((LeagueMainActivity)getActivity()).getLeague();
View v = inflater.inflate(R.layout.diaglog_add_match, null);
Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner_dialog_player1);
Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner_dialog_player2);
String [] items = {"test 1", "test 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("Spinner: ", "" + spinner1);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);
builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
.setTitle("Add match")
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//LoginDialogFragment.this.getDialog().cancel();
/* do I really need to do anything??? */
}
});
AlertDialog dialog = builder.create();
return dialog;
}
This is the code that works within a (tabbed) fragment:
public class UnnamedFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_unnamed, container, false);
Spinner spinner1 = (Spinner) rootView.findViewById(R.id.spinner);
String [] items = {"test 1", "test 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("Spinner: ", "" + spinner1);
spinner1.setAdapter(adapter);
return rootView;
}
}
Okay so what I did wrong was inflating/creating two independent views with the same context.
First I did:
View v = inflater.inflate(R.layout.diaglog_add_match, null);
And then:
builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
So I set the view for the builder as a new view, and not the same ones I used for the spinner.
So instead if I do:
View v = inflater.inflate(R.layout.diaglog_add_match, null);
builder.setView(v)
That does the trick.

Custom listview for AlertDialog

I'm trying to have my AlertDialog with a custom list view but can't seem to get it to show or run without error.
private void buildDialog(){
int selectedItem = -1; //somehow get your previously selected choice
LayoutInflater inflater = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.listview, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
builder.setTitle("Select Weapon").setCancelable(true);
builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
ListView lv = ((AlertDialog) dialog).getListView();
itemId = lv.getAdapter().getItemId(which);
new changeEQ().execute();
}
});
dialog = builder.create();
}
This is my AlertDialog but can't figure out what to add to get my custom layouts, listview & listrow to be used. I've looked around at guides online but nothing they show seems to work for me. IE I must be doing something wrong.
EDIT: changed code to include answer but has no change on what is showed on screen. No errors yet no change in look.
If you have a custom layout that you want to pass to your AlertDialog try:
LayoutInflater inflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.custom_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
If you want to define listeners try:
ListView list = (ListView) customView.findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do as you please
}
});

Can i close an alert dialog on list click?

I have an Alert Dialog which has a list on it, and i want to close onlistclick is it possible?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
ListView myList = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
}
});
builder.setTitle("Please Choose");
builder.setInverseBackgroundForced(true);
builder.setView(myList);
final Dialog dialog = builder.create();
dialog.show();
}
The alert dialog is running perfect i just dont want to put any buttons in it.
If you define the onItemClickListener after the Dialog you can just call dialog.dismiss(); in the onItemClick() method.
check below code
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
dialog.dismiss();
}
});

Categories

Resources