Proceed to next activity through radio buttons on alert dialog - android

I have listview ,on click of each list item, it must pop up a alert with radio buttons. Selecting a radio button option and then clicking "ok" button on alert dialog , I must be able to proceed to next activity. (PS i dont want to use positive , negative button ).
Below is my code, listview is working fine , alert dialog pops up and on selecting yes or no , Toast shows .But upon yes it isn't proceeding to next activity. Please help!!
listview = (ListView) findViewById(R.id.mylistview);
final String[] items = new String[]{"IOS", "ANDROID", "WINDOWS"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemposition = position;
String itemvalue = (String) listview.getItemAtPosition(position);
final CharSequence[] items1 = {"yes", "no"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("slection confirmation");
builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch(items1.toString())
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}

You have problem in below code snippet, As you covert whole string array into string , but you need to get one item at time.
switch(items1.toString())
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
Please replace this with
String selection;
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemposition = position;
String itemvalue = (String) listview.getItemAtPosition(position);
final CharSequence[] items1 = {"yes", "no"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("slection confirmation");
builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selection = items1[which]
Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch(selection)
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});

Related

android listview to editTxt inside a dialog

I have a custom listview , what im trying to do is when a user select a specific value in lisview it will goto editText inside a dialog in the same activity.. but it wont get the value.. here's my code
public void savedNotes(){
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
}
Try this..
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
Hope this will help you.
You can't get the value from a custom listview with
String itemString = list.getItemAtPosition(position).toString();
Instead you might have used an array or an arraylist to store the data that you are setting inside the listview. Inside the onItemCLick() type
//For arraylist
String itemString = your_arrayList.get(position);
//For array
String itemString = your_array[position];
By your code, this "itemString" will appear in edittext only when you click/tap the "Saved" button in the dialog.
You are setting value on EditText after clicking the positive("saved") button of dialog, but when your dialog is already visible EditText is not having any value.
First you need to remove the code from here:-
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
//Remove these two line code from here
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
}
}
Now add these two line just after finding the reference of EditText in alert dialog like :-
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
}

Android - close popUp window

I have a listView inside a popUp window and I want that when the user clicks on an item in the listview, the popUp window will automatically close. Any idea how I can do that?
public void popUp(){
final LayoutInflater layoutInflater = LayoutInflater.from(Record.this);
final View promptView = layoutInflater.inflate(R.layout.input_language, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Record.this);
alertDialogBuilder.setView(promptView);
String[] languages = {"Arabic","Bulgarian","Catalan"};
ListView list = (ListView) promptView.findViewById(R.id.inputlang);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, languages);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String from_language = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), from_language, Toast.LENGTH_LONG).show();
// CLOSE POPUP WINDOW
}
});
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Put this code after alert.show();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String from_language = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), from_language, Toast.LENGTH_LONG).show();
// CLOSE POPUP WINDOW
alert.dismiss();
}
});
You can do this by changing the order of initialization of the dialog:
public void popUp(){
final ListView promptView = new ListView(this);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
final AlertDialog alert = alertDialogBuilder.create();
String[] languages = {"Arabic","Bulgarian","Catalan"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, languages);
promptView.setAdapter(adapter);
promptView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String from_language = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), from_language, Toast.LENGTH_LONG).show();
// CLOSE POPUP WINDOW
alert.dismiss();
}
});
alert.show();
}

setOnSelectedListener doesn't work on gridView

I looked for some answers about onItemSelected but all I found was that they needed to use onItemClick.
In my application, i want that the user will select his name that displays on the gridView, then Click enter, and appears the alertDialog for entering his password.
Tried something but it didn't work for me, here is the relevant code:
waiterList = new ArrayList<Waiter>();
waiterList = dataBaseHelper.showWaiters();
adapter = new MyAdapter(MainActivity.this, waiterList);
gridView.setAdapter(adapter);
gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
waiter = waiterList.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
And the function that calls the click on the button:
public void Enter(View view) {
if (gridView.isSelected()){
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.password_dialog, null);
AlertDialog.Builder passwordDialog = new AlertDialog.Builder(MainActivity.this);
passwordDialog.setTitle(getString(R.string.get_id_uniq));
passwordDialog.setMessage(getString(R.string.enter_id));
passwordDialog.setView(dialogLayout);
passwordDialog.setPositiveButton(getString(R.string.next), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
password = input.getText().toString();
if (password.equals(waiter.getPass())) {
startActivity(new Intent(getApplicationContext(), Activity_Zone.class));
Toast.makeText(getApplicationContext(),
"match", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Wrong pass", Toast.LENGTH_LONG).show();
}
}
});
passwordDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = passwordDialog.show();
input = (EditText) dialog.findViewById(R.id.editText);
}
else {
Toast.makeText(getApplicationContext(), "Waiter isnt selected", Toast.LENGTH_LONG).show();
}
}
remove gridView.isSelected(). This will return you view state.

List Item longClickListener open dialog is not working in android?

i am working on list , in this list setOnItemLongClickListener write the code part ,user long press to open dialog but dialog is not open ?please send any suggestion for open dialog?
listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
view.setSelected(true);
TextView tv = (TextView) view.findViewById(R.id.txtName);
String shipmenttxt = tv.getText().toString();
int b=delete_Message("Delete ", "Do you want delete shipment id", "Delete", "Cancel",shipmenttxt,position);
if(b==1){
this.mList.remove(position);
adapter.notifyDataSetChanged();
}
return true;
}
});
#SuppressWarnings("deprecation")
private int delete_Message(String sTitle,String sMessage,String sButton1_Text,String sButton2_Text,final String msg,final int position)
{
try {
alertDialog = new AlertDialog.Builder(getParent()).create();
alertDialog.setTitle(sTitle);
alertDialog.setIcon(R.drawable.info);
alertDialog.setMessage(sMessage);
alertDialog.setButton(sButton1_Text, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
aa=1;
delete(msg);
//new LoadDatashipment().execute();
return ;
} });
alertDialog.setButton2(sButton2_Text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
aa=0;
//return;
}});
alertDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return aa;
}
If any row item of list contains focusable or clickable view then your click listener might not work properly
you must put this line in your custom listviews row_item.xml file
i.e. android:descendantFocusability="blocksDescendants"
For eg:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
//other layout info here .....
>
</LinearLayout>
i think what you need to do is before showing your Dialog
alertD = alertDialog.create();
and show
alertD.show();
check here for example
http://www.mkyong.com/android/android-alert-dialog-example/
try this code:
listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
TextView tv = (TextView) view.findViewById(R.id.txtName);
String shipmenttxt = tv.getText().toString();
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
//TODO Yes button clicked
this.mList.remove(position);
adapter.notifyDataSetChanged();
break;
case DialogInterface.BUTTON_NEGATIVE:
//TODO No button clicked
dialog.dismiss();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
builder.setMessage("Extract " + rarListView.getItemAtPosition(arg2).toString() + " \n to '" + Environment.getExternalStorageDirectory().toString() + "/AndRar/' folder?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener)
.show();
return true;
}
});

Long Click on a list view and a delete dialog

I am using onlongitemclick and can produce a dialog that comes up to confirm a delete, but I cannot get the listitem position or text.
Edit: I cannot get the selectedValue string value inside of the public void onClick(DialogInterface dialog, int which) function.
lv is my listview object
lv.setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
ListView list1 = (ListView) findViewById(android.R.id.list);
final String selectedValue = (String) list1.getItemAtPosition(arg2);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(RecipeList.this);
alertDialog.setTitle("Delete");
alertDialog.setMessage(selectedValue);
alertDialog.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
obj Recipe = new obj(selectedValue, RecipeList.this);
Recipe.remove(<I need the listview item to create the object and then delete some listing in the DB, seletecValue should do this, but it does not>)
Intent intent2 = new Intent(RecipeList.this, RecipeList.class); //go to recipe list
startActivity(intent2);
} });
alertDialog.setPositiveButton("Keep", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// alertDialog.dismiss();
} });
alertDialog.show();
return true;
}
});
arg1 is your view, you should be able to get the text from the view.
arg2 is the position.
See: http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html

Categories

Resources