How to display an arraylist in AlertDialog - android

i'm trying to get an arraylist on alert dialog but i can see the list items only if i click them. any idea whats wrong with the below code .any suggestions pls....
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final ArrayAdapter<String> aa1=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice, matches);
builder.setSingleChoiceItems(aa1, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
numbers.add(aa1.getItem(item));
aa.notifyDataSetChanged();
dialog.dismiss();
}
});
alert = builder.create();
alert.show();

I believe this is caused by a bug when using the default themes and AlertDialog.Builder.
You should be able to work around it by copying the android.R.layout.simple_list_item_single_choice layout xml out of the platform and creating a local layout file with the android:textColor properties overridden to something other than themed text color attributes.

Just use this one
android.R.layout.simple_spinner_dropdown_item

simple dialog here try this. Just you have to pass string or charsequence array to it shows simple dialog..

Related

AlertDialog with a list shows empty dialog

In my Android app I have a dialog that supposed to show a list:
final AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("Pick a branch to navigate to:");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
adapter.addAll(branches);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + branches.toArray()[which]));
startActivity(searchAddress);
}
});
builder.show();
When I launch this code, I get a dialog with the title, and blank space below, but when I click the blank space it does the action and uses what it got right, so something wrong with the view.
What can I do with that?
If your onClick actions doing fine it means that List filled properly only problem with showing views. So check out your adapter class.
EDIT:
Change Application Context to Activity's Context

Which layout will make a similar dropdown list like standard listbox dropdown list?

I found and tried this code from developer.android.com to create a spinner populated from a string-array resource :
ArrayAdapter<CharSequence> produits_adapter = ArrayAdapter.createFromResource(this, R.array.produits_array, android.R.layout.simple_spinner_item);
produits_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I don't like the design of the dropdown list after clicking the arrow of the spinner :
I want a dropdown list like standard listbox's dropdown list which is also situated below the spinner. So what is the best layout for that instead of android.R.layout.simple_spinner_dropdown_item ?
You may implement your own structure easily with TextView and ListView
You can use AlertDialog.Builderfor showing list.
AlertDialog dialog;
CharSequence[] items = {"A", "B", "C"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("List")
.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if(item==0){
}
else if(item==1){
}
else if(item==2){
}
}
});
dialog=builder.create();
dialog.show();

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.

simplecursoradapter not populated in setSingleChoiceItems AlertDialog

AlertDialog always show empty, here is the code when I pass String array instead of adapter its works but with adapter its empty. Am I missing something?
.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Show dialog");
int selected =0; // select at 0
SimpleCursorAdapter Adapter =new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_list_item_single_choice,
null, new String[]{"title","title2"}, new int[]{android.R.id.text1});
builder.setSingleChoiceItems(
Adapter,
selected ,
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog,
int which) {
Toast.makeText(
MainActivity.this,
"Select "+choiceList[which],
Toast.LENGTH_SHORT
).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Sorry for the late reply. I found your question while facing a similar issue. Why passing a cursorAdapter if you're not actually getting data from a database? I would simply create a new ArrayAdapter. Have you tried that?
ArrayAdapter<String> Adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_singlechoice, new String[]{"title","title2"});
Note that I am using "android.R.layout.select_dialog_singlechoice" instead of "android.R.layout.simple_list_item_single_choice", because your list is inside an AlertDialog. Hope this helps!
Tom.

Android Listview dialog doesn't disapear after choosing an item

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

Categories

Resources