Hi,
I have created the MultiChoice
AlertDialog The AlertDialog has five
list items with checkboxes. When I
check First checkbox, w.r.t this the
if the other checkboxes in the list
are checked they shud be unchecked
automatically and vice versa.
I am checking the isChecked status
in the onClick method of
OnMultiChoiceClickListener() and calling the
showDialog(DIALOG_MULTIPLE_CHOICE); by updating boolean[]
checkedItems; to recreate the
Dialog, But I am unable to achieve it.
If you any suggestions please direct
me to right way.
Is there any way to recreate the AleartDialog onClick event of the radio button click.
Some Sample Code below:
case DIALOG_MULTIPLE_CHOICE:
final String[] lJobTypes = { "Item1", "Item2", "Item3","Item4", "Item5" };
return new AlertDialog.Builder(JoblistPage.this)
// .setIcon(R.drawable.logo)
.setTitle("Title Here")
// .setCustomTitle(m_Title)
.setMultiChoiceItems(lTypes, m_Selections,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,int whichButton, boolean isChecked) {
/* User clicked on a check box do some stuff */
if (isChecked) {
m_CheckCount++;
//Toggle the Radio button Check status
} else {
m_CheckCount--;
}
}
}).setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
Regards
Vinayak
Don't recreate the dialog, just toggle the checkboxes within the current dialog. Your onMultiChoiceClickListener can keep track of the currently active checkbox (if any) and uncheck it when another is selected. Here's a complete tested, working example:
package com.stackoverflow.beekeeper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
public class StackOverflowTest extends Activity {
/** Called when the activity is first created. */
#Override public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private int mSelected = -1;
#Override protected void onResume() {
super.onResume();
final Builder build = new Builder(this);
build.setTitle("List selection");
build.setCancelable(true);
final String[] strings = new String[]{"Cow", "Horse", "Goat"};
final OnMultiChoiceClickListener onClick =
new OnMultiChoiceClickListener() {
#Override public void onClick(final DialogInterface dialog,
final int which, final boolean isChecked) {
if (isChecked) {
if ((mSelected != -1) && (mSelected != which)) {
final int oldVal = mSelected;
final AlertDialog alert = (AlertDialog)dialog;
final ListView list = alert.getListView();
list.setItemChecked(oldVal, false);
}
mSelected = which;
} else
mSelected = -1;
}
};
build.setMultiChoiceItems(strings, null, onClick);
build.setPositiveButton("Done", new OnClickListener() {
#Override public void onClick(final DialogInterface dialog,
final int which) {
String message = null;
if (mSelected == -1)
message = "You didn't select anything.";
else
message = "You selected '" + strings[mSelected] + "'";
Toast.makeText(StackOverflowTest.this, message, Toast.LENGTH_LONG).show();
}
});
build.show();
}
}
One thing to watch for: you must specify "null" for the "checkedItems" parameter in your "setMultiChoiceItems" call -- otherwise the "setItemChecked" calls won't work as expected. It would end up using that array to store the checked state, and "setItemChecked" would'nt update it correctly, so everything would get confused. Odd, but true.
I was struggling with this for quite some time. I maintain an array of the "checked" status of each of the items and change that value while visually changing the setItemChecked value. Then when the done button is clicked I iterate through "checked" to save the values to my db.
builder.setMultiChoiceItems(cats, checked, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item, boolean check) {
if(cats[item].equals("All Categories")) {
AlertDialog d = (AlertDialog) dialog;
ListView v = d.getListView();
int i = 0;
while(i < cats.length) {
v.setItemChecked(i, check);
checked[i] = check;
i++;
}
}
checked[item] = check;
}
});
Did you try replacing setMultiChoiceItems to setSingleChoiceItems in your dialog?
Related
I have a multi choice list inside an AlertDialog.
Reading the documentation of CHOICE_MODE_SINGLE, I thought that you could have one or no item checked but for me it behaves like a Radio Button List. It starts with all checkboxes unchecked by default by once I check one, it cannot be unchecked.
I tried hacking it with manual setItemChecked inside onClick but that is not a solution.
What am I doing wrong? How to achieve one or no checkbox in a ListView?
Here's my code:
builder.setMultiChoiceItems(titles, new boolean[titles.length], new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int position, boolean b) {
if (selectedId == -1) {
selectedId = position;
} else {
if (selectedId == position) {
mDialog.getListView().setItemChecked(position, false);
selectedId = -1;
} else {
mDialog.getListView().setItemChecked(selectedId, false);
selectedId = position;
}
}
}
});
mDialog = builder.create();
mDialog.getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
your code isn't working because the method that you are using, setItemChecked, doesn't change the selected state when receive a false and is working on CHOICE_MODE_SINGLE, which is the normal behaviour of a group of radio buttons. You can see it by yourself with "Go To Implementation" in Android Studio (Ctrl + RightClick over the method).
Also, it's not recommended to use checkboxes for a single choice selector as it will confuse your users. You can easily get radio buttons replacing setMultipleChoiceItems by setSingleChoiceItems. It also apply the single choice mode to your ListView, so you can get rid of your last line.
To allow the user to perform an empty selection with radio buttons you have mainly 2 options:
Add an extra items to your list representing the empty selection option. Label it as "None", "Uncheck" or something similar
Add an extra button to your dialog to dismiss the dialog and return an empty selection.
Here you have a sample of implementation of the first option adding dynamically the empty item for a better re-usability ;)
Screenshot
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String title = "Select your favourite language";
String[] items = {"English", "Spanish", "Chinese", "Java"};
String emptyItemTitle = "NONE OF THEM";
int initialSelection = 0;
showSingleChoiceDialogWithNoneOption(title, items, initialSelection, emptyItemTitle);
}
private void showSingleChoiceDialogWithNoneOption(String title, final String[] titleItems, int initialSelection, String emptyItemTitle ) {
final String[] extendedItems = addEmptyItem(titleItems, emptyItemTitle);
final int[] selectedPosition = {initialSelection};
new AlertDialog.Builder(this)
.setTitle(title)
.setSingleChoiceItems(extendedItems, initialSelection, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selectedPosition[0] = which;
Log.d("MyTag", String.format("Selected item '%s' at position %s.", extendedItems[which], which));
}
})
.setNegativeButton("Cancel", null)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.d("MyTag", String.format("Confirmed the selection of '%s' at position %s.", extendedItems[selectedPosition[0]], selectedPosition[0]));
onSelectionConfirmed(selectedPosition[0]);
}
})
.show();
}
#NonNull
private String[] addEmptyItem(String[] titleItems, String emptyTitle) {
String[] tempArray = new String[titleItems.length + 1];
tempArray[0] = emptyTitle;
System.arraycopy(titleItems, 0, tempArray, 1, titleItems.length);
return tempArray;
}
private void onSelectionConfirmed(int position) {
if (position==0){
//Handle your empty selection
}else{
//Selected item at position
}
}
}
I am trying to send one item of a list on being long clicked to another activity's list.But the second activity i.e MySchedule doesnt update beyond one item.
Here's My code
Activity from where i am sending the string
(didnt added the code of string)
public class CloudEvents extends AppCompatActivity {
static int scheduleId = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(CloudEvents.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Save Event")
.setMessage("Do you want to save this event into your schedule?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
scheduleId++;
Toast.makeText(CloudEvents.this,"Saved",Toast.LENGTH_LONG).show();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("CloudEvent",listView.getItemAtPosition(position).toString()).apply();
Intent i = new Intent(CloudEvents.this,MySchedule.class);
startActivity(i);
//myArrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No",null)
.show();
return true;
}
});
Activity Receiving the string and making a list
public class MySchedule extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_schedule);
final ArrayList<String> schedule = new ArrayList<>();
final ListView scheduleListView = (ListView)findViewById(R.id.scheduleListView);
String key = "CloudEvent";
String myEvent ="";
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
if(sharedPreferences.contains(key))
{
myEvent = sharedPreferences.getString(key,"");
schedule.add(CloudEvents.scheduleId,myEvent);
}
final ArrayAdapter myArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,schedule);
scheduleListView.setAdapter(myArrayAdapter);
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MySchedule.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure ?")
.setMessage("Do you want to delete this note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
schedule.remove(position);
myArrayAdapter.notifyDataSetChanged();
CloudEvents.scheduleId--;
}
})
.setNegativeButton("No",null)
.show();
return true;
}
});
}
}
(after adding one item)
Error:Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:457)
at com.yatin.whatshappeningdtu.MySchedule.onCreate(MySchedule.java:35)
Being racking my brain for hours now.Please Help Thanks !
When you add item first time, your CloudEvents.scheduleId is set to 0 from -1. Suppose your string is "FirstEvent" that you save in CloudEvent key of sharedpreference, and then In MySchedule activity it is added in 0 position of schedule arraylist, and it works fine.
Now when you come back to CloudEvents activity, your CloudEvents.scheduleId is 0 because it's statica variable, and you add another item let's say "SecondEvent", so CloudEvents.scheduleId will change from 0 to 1, and you are saving "SecondEvent" string in CloudEvent key again in sharedpreference, so that previous value "FirstEvent" will override with "SecondEvent" that means in MySchedule activity you will only get "SecondEvent" from sharedpreference, but you are adding this in 1st position of schedule arraylist and 0th position of schedule arraylist will be left null.
Now you are passing this arraylist, with null value in 0th position, in Listview adapter, that's way it is throwing IndexOutOfBoundsException exception.
To solve this issue, you can maintain one ArrayList<String>.In CloudEvents activity make following changes.
private ArrayList<String> eventList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState){
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
Collections.addAll(eventList, prefManager.getString("CloudEvent", "").split(","));
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(CloudEvents.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Save Event")
.setMessage("Do you want to save this event into your schedule?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
scheduleId++;
eventList.add(listView.getItemAtPosition(position).toString());
Toast.makeText(CloudEvents.this, "Saved", Toast.LENGTH_LONG).show();
Intent i = new Intent(CloudEvents.this, MySchedule.class);
i.putStringArrayListExtra("EventList", eventList);
startActivity(i);
//myArrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
}
Now in MySchedule activity make following changes.
public class MySchedule extends AppCompatActivity {
ArrayList<String> schedule = new ArrayList<>();
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_schedule);
sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
schedule = getIntent().getStringArrayListExtra("EventList");
ListView scheduleListView = (ListView)findViewById(R.id.scheduleListView);
final ArrayAdapter myArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,schedule);
scheduleListView.setAdapter(myArrayAdapter);
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MySchedule.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure ?")
.setMessage("Do you want to delete this note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
schedule.remove(position);
myArrayAdapter.notifyDataSetChanged();
String events = "";
for(int i=0; i<schedule.size(); i++){
events = events + schedule.get(i);
if(i != (schedule.size() - 1)){
events = events + ",";
}
}
sharedPreferences.edit().putString("CloudEvent", events).apply();
}
})
.setNegativeButton("No",null)
.show();
return true;
}
});
}
}
Just try commit() instead of apply()
apply() was added in 2.3, it commits without returning a boolean indicating success or failure.
commit() returns true if the save works, false otherwise.
And also please confirm that the instance of shared preference which is used to read the data is same as the instance to which the data is written.
A custom dialog is being used to take in user input, and then these values are being passed to another activity using getter methods.
But when I pass the values to a method that outputs the string values to a CSV file, shipName, analystName etc the values appear as empty in the file like this, " " although I have entered the values in the dialog.
I debugged the problem by watching the String values in the debug menu's expression window, shipName and analystName but the values never update in the expression window.
I gather from this that the method i which the input is being passed over is not correct.
Does anyone know why the values being output are empty?
This the dialog class being used:
package ie.gmi.computing;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.renderscript.Sampler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyMessageDialog {
private Context context;
private EditText shipText, scientistNameText , scientistEmailText , volumeText , colourText ;
private String shipString, scientistNameString , scientistEmailString , volumeString , colourString ;
public AlertDialog displayMessage(Context context, String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
LayoutInflater inflater = LayoutInflater.from(context);
final View v = inflater.inflate(R.layout.custom_view, null);
builder.setView(v);
shipText = (EditText)v.findViewById(R.id.shipNameEditText);
scientistNameText = (EditText)v.findViewById(R.id.scientistEditText);
scientistEmailText = (EditText)v.findViewById(R.id.emailEditText);
volumeText = (EditText)v.findViewById(R.id.volumeEditText);
colourText = (EditText)v.findViewById(R.id.colourEditText);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog));
return dialog;
}
//getter/setters to allow access to string values
//in SearchResult class
public EditText getShipText() {
return shipText;
}
public void setShipText(EditText shipText) {
this.shipText = shipText;
}
public EditText getScientistNameText() {
return scientistNameText;
}
public void setScientistNameText(EditText scientistNameText) {
this.scientistNameText = scientistNameText;
}
public EditText getScientistEmailText() {
return scientistEmailText;
}
public void setScientistEmailText(EditText scientistEmailText) {
this.scientistEmailText = scientistEmailText;
}
public String getShipString() {
return shipString;
}
public void setShipString(String shipString) {
this.shipString = shipString;
}
public String getScientistNameString() {
return scientistNameString;
}
public void setScientistNameString(String scientistNameString) {
this.scientistNameString = scientistNameString;
}
public String getScientistEmailString() {
return scientistEmailString;
}
public void setScientistEmailString(String scientistEmailString) {
this.scientistEmailString = scientistEmailString;
}
public String getVolumeString() {
return volumeString;
}
public void setVolumeString(String volumeString) {
this.volumeString = volumeString;
}
public String getColourString() {
return colourString;
}
public void setColourString(String colourString) {
this.colourString = colourString;
}
public EditText getVolumeText() {
return volumeText;
}
public void setVolumeText(EditText volumeText) {
this.volumeText = volumeText;
}
public EditText getColourText() {
return colourText;
}
public void setColourText(EditText colourText) {
this.colourText = colourText;
}
#SuppressLint("NewApi")
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
if(shipText.getText().toString().isEmpty() && !shipText.getText().toString().equals(null)){
shipText.setError("The Field is required");
}else if(scientistNameText.getText().toString().isEmpty() && !scientistNameText.getText().toString().equals(null)){
scientistNameText.setError("The Field is required");
}else if(scientistEmailText.getText().toString().isEmpty() && !scientistEmailText.getText().toString().equals(null)){
scientistEmailText.setError("The Field is required");
}else if(volumeText.getText().toString().isEmpty() && !volumeText.getText().toString().equals(null)){
volumeText.setError("The Field is required");
}else if(colourText.getText().toString().isEmpty() && !colourText.getText().toString().equals(null)){
colourText.setError("The Field is required");
}else{
shipText.setError(null);
scientistNameText.setError(null);
scientistEmailText.setError(null);
volumeText.setError(null);
colourText.setError(null);
shipString = shipText.getText().toString();
scientistNameString = scientistNameText.getText().toString();
scientistEmailString = scientistEmailText.getText().toString();
volumeString = volumeText.getText().toString();
colourString = colourText.getText().toString();
Toast.makeText(dialog.getContext(), "The Values you get from : " +
"\n Ship name value: " + shipText.getText().toString() +
"\n Scientist name value: " + scientistNameText.getText().toString() +
"\n email value: " + scientistEmailText.getText().toString() +
"\n sample volume value: " + volumeText.getText().toString() +
"\n sample colour value: " + colourText.getText().toString() , Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
}
}
And this is how I'm retrieving the values in my SearchResult class, when i select the settings button:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
MyMessageDialog dialog =new MyMessageDialog();
dialog.displayMessage(SearchResult.this, "Sample Info", "Required");
// store / use the values here
shipName = dialog.getShipString();
analystName = dialog.getScientistNameString();
analystEmail = dialog.getScientistEmailString();
sampleVolume = dialog.getVolumeString();
sampleColour = dialog.getColourString();
longitudeValue = String.valueOf(lng);
latitudeValue = String.valueOf(lat);
sampleMaterial = message;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Your dialog showing is asynchronous; that is, code execution in onOptionsItemSelected() does not pause after the call to dialog.displayMessage(), so the getters are returning the initial values for those fields, which is null in all cases. You should create an interface that the Activity implements to receive a callback after those fields are set in the onClick() method of your CustomListener, and update the Activity's variables then.
In the dialog class, we create an interface. For example:
public class MyMessageDialog {
public interface DialogCallback {
public void onValuesSet();
}
...
}
Be sure to save a reference to the Context:
public AlertDialog displayMessage(Context context, String title, String message){
this.context = context;
...
}
And at the end of the onClick() method, after the fields' values are set:
((DialogCallback) context).onValuesSet();
The Activity needs to implement the interface we created, the dialog should be a class member, and the fields will be set in the interface's callback method:
public class SearchResult extends Activity
implements MyMessageDialog.DialogCallback {
...
MyMessageDialog dialog;
#Override
public void onValuesSet()
{
shipName = dialog.getShipString();
analystName = dialog.getScientistNameString();
...
}
...
}
Does anyone know why the values being output are empty?
Because all statements from getting data from dialog class is executing after dialog.displayMessage on menu option section.
How to get data from MyMessageDialog on Ok button click ?
1. Instead of getting EditText from MyMessageDialog change all getter/setter method return type to String..
2. Create a event Listener using interface for getting event of Alert close on Ok button click in Activity. you can create event listener as:
Android Custom Event Listener
3. call all setter method on Ok button click of Alert. after calling all setter method call event listener method to execute event in Activity after Alert finish.:
#Override
public void onClick(DialogInterface dialog, int which) {
setShipText(shipText.getText().toString());
setScientistNameText(shipText.getText().toString());
....
//..close alert and call event listener method
}
Your getters don't seem to actually call the getText() method on each EditText view. Modify them do that they do (e.g., scientistNameText.getText()).
PS And, yes, Mike M may indeed write about asyncronicity contributing to the problem. Another approach around that would be to add something like this to the code for each EditText view:
myEditTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//Set the variable here that you call in your getter. Use `getText()` to get the string (e.g., myGetterVariable = myEditTextView.getText().
}
}
When implementing setMultiChoiceItems with a cursor, you have to specify an isCheckedColumn.
The problem, as articulated on other sites, is that when users select an item from the list the checkbox does not update. Some have suggested updating the SqLite table each time a user selects an item, but this did not work in my application. Here is the solution I came up with.
This is what I came up with:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int myDialogChoice = getArguments().getInt("whichDialog");
mSelectedItems = new ArrayList(); // Where we track the selected items
mCurrentFavoritesSelection = new ArrayList();
myDataBaseAdapter = new AthleteDbAdapter(getActivity());
// int myAthleteId;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
switch(myDialogChoice) {
case Select_From_Favorites:
myCursorFromSqLite = myDataBaseAdapter.fetchAllFavorites(getActivity());
// You need a Primative Boolean Array to specify which items were selected last time.
boolean[] booleanPrimativeArray = new boolean[myCursorFromSqLite.getCount()];
final ArrayList mArrayListOfIDs = new ArrayList();
ArrayList<Boolean> myBooleanList = new ArrayList<Boolean>();
// This array will be the choices that appear in the Dialog.
ArrayList<String> mArrayListOfNames = new ArrayList<String>();
myCursorFromSqLite.moveToFirst();
/* Populate Arrays
*
*/
int iCount = 0;
while(!myCursorFromSqLite.isAfterLast()) {
// put _id's from SqLite data into an array.
mArrayListOfIDs.add(Integer.valueOf(
myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex(KEY_ROWID))));
// put series of booleans into Primative Array depending upon whether user selected them last time.
if(Integer.valueOf(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex("checked"))) == 1){
booleanPrimativeArray[iCount] = true;
mSelectedItems.add(
Integer.valueOf(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex(KEY_ROWID)))
);
// I kept track of what selections from last time were.
mCurrentFavoritesSelection.add(
Integer.valueOf(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex(KEY_ROWID)))
);
} else booleanPrimativeArray[iCount] = false;
iCount++;
mArrayListOfNames.add(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex("fullName")));
myCursorFromSqLite.moveToNext();
}
// Change the ArrayList of names to a Char Sequence
CharSequence[] charSeqOfNames = mArrayListOfNames.toArray(new CharSequence[mArrayListOfNames.size()]);
try{
myCursorFromSqLite.close();
} catch (Throwable t) {
Log.e(APP_TAG,"Error closing myCursorFromSqLite Cursor " + t);
}
builder.setTitle(R.string.pick_athletes)
.setMultiChoiceItems(charSeqOfNames, booleanPrimativeArray,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, build an array containing the selected items _id's.
mSelectedItems.add((Integer) mArrayListOfIDs.get(which));
} else if (mSelectedItems.contains((Integer) mArrayListOfIDs.get(which))) {
// Else, if the user changes his mind and de-selects an item, remove it
mSelectedItems.remove((Integer) mArrayListOfIDs.get(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.pullathletesbutton, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
Log.d(APP_TAG,"Call something");
mListener.onDialogPositiveClick(PickListDialog.this, mSelectedItems, mCurrentFavoritesSelection);
}
})
.setNegativeButton(R.string.cancelbutton, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
This worked well. The user can change his mind without affecting the underlying database and the checkmarks update properly. Once the user has finalized his choices, he hits the "positive" button and the database is updated.
In My application one button is there when you click on that one alert dialog will be appear. that alert dialog consists of single choice list items. Here i want to set the text size of single choice list item.
is it possible? if yes how to do it.
The following is my code
sclist.java
package com.examples.scl;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class sclist extends Activity {
private static final int DIALOG_SINGLE_CHOICE = 1;
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_SINGLE_CHOICE:
return new AlertDialog.Builder(sclist.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("Single choice list")
.setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked on a radio button do some stuff */
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
}
return null;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Display a radio button group */
Button radioButton = (Button) findViewById(R.id.radio_button);
radioButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_SINGLE_CHOICE);
}
});
}
}
I just encountered this problem myself on a matching game I'm working on. My solution isn't simple but I wanted to use a custom font, and I didn't see an easy way to do it with the 2.2 Android interface (which is what I'm targeting). The trick is to attach an OnShowListener to the alert dialog before you show it. In that listener, get ListAdapter out of the ListView and wrap it with a proxy object that forwards all the calls except the getView. In that function, cast the View to a TextView, set the typeface and size, and return the view. Here's my code:
// Add your list with builder up here
AlertDialog alert = builder.create();
alert.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface alert) {
ListView listView = ((AlertDialog)alert).getListView();
final ListAdapter originalAdapter = listView.getAdapter();
listView.setAdapter(new ListAdapter()
{
#Override
public int getCount() {
return originalAdapter.getCount();
}
#Override
public Object getItem(int id) {
return originalAdapter.getItem(id);
}
#Override
public long getItemId(int id) {
return originalAdapter.getItemId(id);
}
#Override
public int getItemViewType(int id) {
return originalAdapter.getItemViewType(id);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = originalAdapter.getView(position, convertView, parent);
TextView textView = (TextView)view;
textView.setTypeface(MyFontUtil.getTypeface(MyActivity,MY_DEFAULT_FONT));
textView.setTextColor(Color.BLACK);
textView.setTextSize(25); // FIXIT - absolute size
return view;
}
#Override
public int getViewTypeCount() {
return originalAdapter.getViewTypeCount();
}
#Override
public boolean hasStableIds() {
return originalAdapter.hasStableIds();
}
#Override
public boolean isEmpty() {
return originalAdapter.isEmpty();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
originalAdapter.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
originalAdapter.unregisterDataSetObserver(observer);
}
#Override
public boolean areAllItemsEnabled() {
return originalAdapter.areAllItemsEnabled();
}
#Override
public boolean isEnabled(int position) {
return originalAdapter.isEnabled(position);
}
});
}
});
alert.show();
If you want to see it in action look on the Android Market in a few weeks. Search for metaphyze (my publisher id). I haven't decided what to call it yet. (It's not "FlashMatch Chinese I Free". That was my first game. This is a kid's matching game. Play the game and tap the picture at the end. You'll see the AlterDialog with the style list.).
Good question. I believe you'd have to use the AlertDialog.Builder constructor that also takes a theme AlertDialog.Builder(Context context, int theme), see if you can see anything useful in that description, I've never tried it myself.