Can i close an alert dialog on list click? - android

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();
}
});

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
}
});

How to dissmiss alert dialog whem list item click in android

Here is my code:
When I click on textview one dialog displaying list is shown. When i select particular list item dialog is not dismissed. how to dismiss dialog when list item is clicked,
educationtxt=(TextView)findViewById(R.id.education_txt);
String[] educationarray = new String[]{"High School","Som College","Associates Degree","Bachelor Degree","Masters Degree","PHD"};
educationtxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final ListView lv ;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegistrationActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Education");
View convertView1 = (View) inflater.inflate(R.layout.custom_dialog_row, null);
TextView tv =(TextView)convertView1.findViewById(R.id.list_row_txt);
lv = (ListView) convertView.findViewById(R.id.custom_listView1);
lv.setBackgroundColor(Color.WHITE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(RegistrationActivity.this,R.layout.custom_dialog_row,R.id.list_row_txt,educationarray);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
education_selected = lv.getAdapter().getItem(position).toString();
educationtxt.setText(education_selected);
Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
//here i want dismiss
}
});
alertDialog.show();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
education_selected = lv.getAdapter().getItem(position).toString();
educationtxt.setText(education_selected);
Toast.makeText(RegistrationActivity.this, "You Clicked at "+education_selected, Toast.LENGTH_SHORT).show();
//here i want dismiss
alertDialog.dissmiss(); <----- add this lone to dismiss
}
});
and make alertdialoge as a final
Just Call this method and Show Dialog. Also Dismiss Dialog On Click ListItem
public void showDialog(){
final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
dialog.setTitle("Choose App");
dialog.setCancelable(true);
View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_dialog_all_app, null);
list = (ListView) view.findViewById(R.id.AllAppList);
AllAppPckName = getPackages();
AllAppListAdapter adapter= new AllAppListAdapter(getContext(), R.layout.app_item, AllAppPckName);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
appIcon.setVisibility(View.VISIBLE);
appIcon.setImageDrawable(getPackageIcon(getContext(), AllAppPckName.get(i)));
appNameBtn.setText(getAppNameFromPkgName(getContext(), AllAppPckName.get(i)));
dialogg.dismiss();
}
});
dialog.setView(view);
dialogg = dialog.show();
}

show an open spinner

I have Async task to provide me list of some cities and after I have the list, I want to show me an OPEN spinner directly without any dialogs. My code opens spinner with propper list, but clickListener doesn't seems to work.
my code:
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerCities);
listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = new Spinner(this);
spinner.setAdapter(listAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> aView, View selectedItemView,
int position, long longID) {
System.out.println("ON ITEM CLICK LISTENER HERE");
}
public void onNothingSelected(AdapterView<?> aView) {
}
});
What I am missing here?
So after several hours, I figured it out. I need to use Dialog, but result is the same I have wanted. Here is source in case, someone is find useful.
private void MyMethod(){
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
(String[])MyClass.getListOfCities().toArray());
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(icon);
ad.setTitle("Title");
ad.setSingleChoiceItems( listAdapter, -1, new OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
// DO something when I click on item
dialog.dismiss();
}
});
ad.show();
}

listview open alert dialog depending on choice android

Im desperately trying to get my listView to open an alert dialog or a normal dialog that is filled with information. I cant seem to get it to work. I want it to display different information aswell depending on which item on the list is clicked
public class learn_tab1 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
BASICLIST));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setCancelable(false);
dialog.setTitle("Instructions");
dialog.setIcon(R.drawable.bone_icon);
dialog.setMessage("test");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
}
}
});
}
Try new AlertDialog.Builder(learn_tab1.this).create(); instead of new AlertDialog.Builder(this).create().
I'm wondering how it ever get complied....
EDIT
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String)parent.getItemAtPosition(position);
// ...
dialog.setMessage(item);
// ...
}

Categories

Resources