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
Related
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;
}
}
I'm working on an android app and my requirement is that, I need to display the cursor values into alertdialog and let the user choose an item from the list and the value selected should be returned to the calling Activity.In my app, based on the student info, the cursor holds the values of courses he is taking. So the user should be able to choose one of the courses and then that value should be returned to the Activity that called alertdialog. Can you please let me know how to proceed on this.I've looked at multiple examples and none seems to work exactly.
Here is my sample code
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
final Cursor courses=dbConnector.getCourses(student);
builder.setTitle("Enter Course");
builder.setCursor(courses, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int itemSelected) {
if (courses.moveToPosition(itemSelected)) {
String text=courses.getString(0);
Toast.makeText(getApplicationContext(),
"You selected: "+text,
Toast.LENGTH_SHORT);
builder.show();
}
courses.moveToFirst();
}
},"course");
builder.create();
createRow(sview, student, pass,text);
Now I want to return the text variable to the Calling Activity but, here it is local to the onClick() method. How can we do that without having to extend any DialogFragment class.
I suggest you calling a method of the class inside overridden onClick method and do whatever you like with that because the method will be in the Activity like here
#Override
public void onClick(DialogInterface dialogInterface, int itemSelected) {
myMethod(itemSelected);
}
private void myMethod(int selectedItem){
//Do whatever with that selected item
}
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();
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case Dialog_alert:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final LayoutInflater li = LayoutInflater.from(this);
builder.setTitle("Choose any option: ");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setContentView(R.layout.tkentry);
// what to do after this..???
This is a dialog which asks the user to choose one of the following.
'items' is the String array which has "Update" and "Delete" options.
How to set separate onClick methods for them ?
"Update" and "Delete" are not BUTTONS!
You don't need two separate onClick method. You have to use int which to know which item is being selected.
If you want to know what item is selected you must add AdapterView.OnItemSelectedListener listener to your AlertDialog.Builder object.
builder.setOnItemSelectedListener (...);
You'll be notified when an item is selected so you'll be able to save it and when the confirm button is pressed you can take any action you want depending on the selected option.
I'm using and ArrayAdapter to populate a ListView. After selecting and item, it displays a confirmation Y/N dialog. If the user's choice is negative, then he should be able to select another item showing the same dialog. And so on.
Here's my code:
lView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {
Toast.makeText("Selected file"+ mFiles.get(index).getfileName(),
Toast.LENGTH_SHORT).show();
SelectedFile = mFiles.get(index);
showDialog(DIALOG_CONFIRMIMPORT_ID);
}
});
The weird thing is that while the "Toast" shows the clicked item every time, only the first selected item since the Activity is initiated is being passed to "SelectedFile". No matter how many times you click a diferent item, "SelectedFile" always assumes the same value, the value of the first clicked item, outside of this code.
Heres's my Dialog code:
Protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_CONFIRMIMPORT_ID:
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String message = String.format(getString(R.string.importstudentfileconfirm),SelectedFile.getfileName());
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Activity.this.finish();
// startActivity(new Intent(Activity.this, LOL.class));
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SelectedFile = null;
dismissDialog(DIALOG_CONFIRMIMPORT_ID);
mFiles.notifyAll();
}
});
AlertDialog alert = builder.create();
return alert;
}
}
return null;
}
Thank you very much for any help!
I'm guessing this has something to do with the fact that the onCreateDialog method is only called the first time the dialog is created. So the first time you see the dialog it will have the correct filename.
After onCreateDialog is called, onPrepareDialog(...) is called. onPrepareDialog, allows you to change the dialog after it has been created, but before it gets displayed.
Remember that underneath everything, Android isn't creating a new Dialog for you every time you want to show the DIALOG_CONFIRMIMPORT_ID dialog. It is too computationally expensive to instantiate a new dialog every time. Instead, it creates it once, which causes onCreatDialog to be called, followed by the onPrepareDialog. Every other time the dialog is shown, it only calls onPrepareDialog.
Check out the following article on the Android Developer site. It explains things pretty clearly.
http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
So try using onCreateDialog just for initialization of stuff that won't change between showings of the dialog, then use the onPrepareDialog method to dynamically update the contents of the dialog (i.e. getting the new filename)
Cheers!