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();
}
});
}
Related
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();
}
});
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();
}
I have an AlertDialog with the GridView to choose a color, now I want to close the AlertDialog when click item . How can I do?
private void Alert_Colori() {
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.color_picker, null);
GridView gridViewColors = (GridView) view.findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(this));
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
}
});
customDialog.setView(view);
customDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
customDialog.setView(view);
customDialog.show();
}
First Add
final Dialog dialog = customDialog .create();
after the line
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
And then add this in your code
dialog.dismiss();
to
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
}
ie, your code for the ItemClickListener will be as follows
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
}
});
Also, please remember to change customDialog.show(); to dialog.show()
Do this - customDialog.dismiss();
And try changing
final AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
to
final AlertDialog customDialog = new AlertDialog.Builder(this);
in your original code like this -
private void Alert_Colori() {
final AlertDialog customDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.color_picker, null);
GridView gridViewColors = (GridView) view.findViewById(R.id.gridViewColors);
gridViewColors.setAdapter(new ColorPickerAdapter(this));
gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here close the dialog
customDialog.dismiss();
}
});
customDialog.setView(view);
customDialog.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
customDialog.setView(view);
customDialog.show();
}
As describe here: https://stackoverflow.com/a/23372134 Get the instance of AlertDialog using create
I have a ListView showing the names of different profiles, when the user holds the click on an item the activity shows an alertDialog, if the user press the confirm button I want to remove the element from my listView, from my ArrayAdapter and from my ArrayList.
I know that arg2 in the onItemLongClick method represent the index of the selected item but I want to be able to access it inside the onClick method of the positive button.
Any advice?
My ArrayList is called "ListaUtentiStringa", the ArrayAdapter is "profilesAdapter" and the listView is called listview.
Sorry for my bad english.
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Vuoi davvero cancellare il profilo?");
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
}
});
builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
profilesAdapter.notifyDataSetChanged();
return true;
}
});
Do this:
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Vuoi davvero cancellare il profilo?");
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
adapter.remove(adapter.getItem(position));
}
});
Try this..
Use ListaUtentiStringa ArrayList and profilesAdapter adapter as Global variable.
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
ListaUtentiStringa.remove(arg2);
profilesAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
EDIT
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
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