dialog.dismiss() doesn't work on item click inside alertDialog - android

Why dialog.dismiss() doesn't work after click on item inside the AlertDialog ?
I have also tried alert.dismiss but i get the error dialog cannot be resolved. How can i solve that?
Here's the dialog
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
builder.setTitle("List");
lv = (ListView) convertView.findViewById(R.id.listView1);
if (prefs.contains("Name") || prefs.contains("NameTwoo"))
{
String[] values = new String[] {name,nameTwoo};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg) {
String selected;
selected = lv.getItemAtPosition(position).toString();
if(selected.equalsIgnoreCase(name))
{
mapView.getMapViewPosition().setCenter(myMarkerGeopoint);
dialog.dismiss();
}
}
});
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // I get error!
}
});
AlertDialog alert = builder.create();
alert.show();
}

Declare the AlertDialog before build it and use it to dismiss.
AlertDialog alert;
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
...
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert = builder.create();
alert.show();
}

Related

Second AlertDialog list item click based on the first Alertdialog list android

In my android app, I need to generate one Alertdialog with a list of Company name. For example Company 1, Company 2, Company 3. Now if the user cllick company 1, he will get second alertdialog which will show some actions. Like Phone Call, Email, etc. Now I have implemented this two alertdialog in my code. But what I want to do, that for each company there should be different Phone number and email adress. So Far I have tried with same number with all the company. But in real in real if user click company 1, he will get the second alert list of action with phone call, email. if he clicks phone option he will see the phone number company 1, if he clicks company 2, he will get alertoption with phone number of company 2. But I am very new in developing area. I know there is something with Mapping topic, by which I can do it easily but I am not getting actually how to proceed with it. My code is like this
public List<CompanyDetail> setCompanydata(){
int n = 3;
private List<CompanyDetail> companyDetailList = new ArrayList<CompanyDetail>(); //modifier private is not allowed here
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>(); //modifier private is not allowed here
for(int i=0;i<n;i++){
private CompanyDetail comD= new CompanyDetail(); //modifier private is not allowed here
comD.setcompanyPhoneNo(companyPhoneno); //cannot resolve problem companyPhone
comD.setcompanyEmail(compnayEmailId);
companyDetailList.add(comD);
companyContactDetail.add(companyname, companyDetailList);//cannot resolve method 'add(?,java util list..
}
return companyContactDetail; //incompatible type
}
private List<CompanyDetail> companyDetailList;
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>();
companyContactDetail = setCompanydata(); //unknown class company contact deatil
private void showFirstDialogwithList() {
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
builder.setCancelable(true);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("Contact");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.company_name);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(arrayAdapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
showSecondDialogwithList();
}
if (itemPosition == 1) {
alert.dismiss();
showSecondDialogwithList();
}
if (itemPosition == 2) {
alert.dismiss();
showSecondDialogwithList();
}
}
});
}
private void showSecondDialogwithList() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
final ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("What do you want to do");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(adapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
if(isPermissionGranted()){
call_action();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemPosition == 1) {
alert.dismiss();
......;
}
if (itemPosition == 2) {
dismiss();
showEmail();
}
if (itemPosition == 3) {
dismiss();
}
}
});
}
My string arrays are
<string-array name="company_name">
<item>company 1</item>
<item>Company 2</item>
<item>Company 3</item>
</string-array>
<!-- AlertDialog way of Contact array -->
<string-array name="contact_way">
<item>Phone Call</item>
<item>Email</item>
</string-array>
<String-array name="phone">
<item>123456</item>
<item>125658</item>
<item>123451</item>
</String-array>
<String-array name="email">
<item>email1</item>
<item>email2</item>
<item>email2</item>
</String-array>
you can pass the position for the item and test it at your function or you can pass it directly :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alert.dismiss();
showSecondDialogwithList(position);
}
private void showSecondDialogwithList(int position) {
String phoneNumber;
switch (position) {
case 1:
phoneNumber = "123";
break;
case 2:
phoneNumber = "456";
break;
case 3:
phoneNumber = "789"
}
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
final ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("What do you want to do");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(adapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
if (isPermissionGranted()) {
call_action();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemPosition == 1) {
alert.dismiss();
......;
}
if (itemPosition == 2) {
dismiss();
showEmail();
}
if (itemPosition == 3) {
dismiss();
}
}
});
}
To go deep in to logic behind that kind of scenario best way to use *HashMap*.
according to your scenario. i change in your code check it out:
public class CompanyDetail{
String companyPhoneNo;
String companyEmail;
public void setcompanyPhoneNo(String phoneNo){
this.companyPhoneNo = phoneNo;
}
public void setcompanyEmail(String Email){
this.companyEmail = Email;
}
public String getcompanyPhoneNo(){
return companyPhoneNo;
}
public String getcompanyEmail(){
return companyEmail;
}
}
public HashMap<String, List<CompanyDetail>> setCompanydata(){
int n = 3;
private List<CompanyDetail> companyDetailList = new ArrayList<CompanyDetail>();
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>();
for(int i=0;i<n;i++){
CompanyDetail comD= new CompanyDetail();
comD.setcompanyPhoneNo(companyPhoneno);
comD.setcompanyEmail(compnayEmailId);
companyDetailList.add(comD);
companyContactDetail.add(companyname, companyDetailList);
}
return companyContactDetail;
}
private List<CompanyDetail> companyDetailList;
private HashMap<String, List<CompanyDetail>> companyContactDetail;
companyContactDetail = setCompanydata();
private void showFirstDialogwithList() {
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
builder.setCancelable(true);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("Contact");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.company_name);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(arrayAdapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
}
if (itemPosition == 1) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
showSecondDialogwithList();
}
if (itemPosition == 2) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
showSecondDialogwithList();
}
}
});
}
private void showSecondDialogwithList( String companyName, List<CompanyDetail> companyDetail) {
CompanyDetail obj = companyDetail.get(0);
private String companyPhone = obj.getcompanyPhoneNo();
private String companyEmail = obj.getcompanyEmail();
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
final ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("What do you want to do");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(adapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(companyPhone);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
if(isPermissionGranted()){
call_action();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemPosition == 1) {
alert.dismiss();
......;
}
if (itemPosition == 2) {
dismiss();
showEmail();
}
if (itemPosition == 3) {
dismiss();
}
}
});
}
It's not a good practice to display two dialogs one by one. Of course you can implement it, like described above, but it will be much better to display separated activity(fragment) with the list(ListView, RecyclerView) of companies and click on each item will show the dialog with needed params. It will look better from design side and users experience.

how to update the contents of the TextView from EditText in alertdialog

I want to update TextView after Button OK on alertdialog clicked but can't.
As from the code, I am using ListView and using method OnItemClickListener() to handle the ItemClick from ListView.
My Code is as below :
lvMainChat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LayoutInflater li = LayoutInflater.from(context);
final View promptsView = li.inflate(R.layout.deskripsi, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText ciperteks2 = (EditText) promptsView
.findViewById(R.id.ciperteks2);
final EditText plainteks2 = (EditText) promptsView
.findViewById(R.id.plainteks2);
final EditText keyOfDeskripsi = (EditText) promptsView
.findViewById(R.id.keyOfDeskripsi);
final Button deskripsi = (Button) promptsView
.findViewById(R.id.btDeskripsi);
ciperteks2.setText((CharSequence) (((TextView) view.findViewById(R.id.txtMsg)).getText()));
result1 = (TextView) findViewById(R.id.txtMsg);
// set dialog message
alertDialogBuilder
.setTitle("Deskripsi Pesan")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String name2 = plainteks2.getText().toString();
if(name2.length() == 0){
Toast.makeText(getApplicationContext(), "You need to type something in the editText", Toast.LENGTH_LONG).show();
}else{
result1.setText(name2);//why it not work
dialog.dismiss();
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
deskripsi.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ciperteks = ciperteks2.getText().toString().trim();
String key2 = keyOfDeskripsi.getText().toString().trim();
String plaintext = plainteks2.getText().toString().trim();
String deKata = "";
if(!plaintext.equals(null)) {
try {
plainteks2.setText("");
deKata = CopyOfZIgZagCode1.RFDecryptionWork(ciperteks, key2);
} catch(Exception e) {}
plainteks2.setText(deKata);
}
if ( key2.isEmpty()) {
Toast.makeText(getApplicationContext(), "Textbox key can not be empty ", 1).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();
}

Spinner from editext android

My Scenario:
When I click the top (+)icon there is a dialog displayed with editext and If I enter some text and click ok button the text should be added to my spinner which I am unable to do it.
Here is what I mean to say:
This is what I have done:
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Spinner element
listsp = (Spinner) findViewById(R.id.listspinner);
listtext = (EditText) findViewById(R.id.list_text);
list = new ArrayList<String>();
list.add(listtext.getText().toString());
listadapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Try to update the adapter outside the onClick :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Whatever else
listsp = (Spinner) findViewById(R.id.listspinned);
list = new ArrayList<String>();
listadapter = new MyArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listtext = (EditText) findViewById(R.id.list_text);
updateAdapter(listtext.getText().toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
protected void updateAdapter(String input) {
list.add(input);
listadapter.notifyDataSetChanged();
}
EDIT : Here's how to implement your custom adapter (I made it private so it'd use the same dataList. Therefore, you don't need to call any updateData() function, just to notify the adapter that the data has changed with notifyDataSetChanged()) :
private class MyArrayAdapter extends BaseAdapter implements SpinnerAdapter {
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(list.get(position).getName());
return text;
}
}

how can i set selected value from alert dialog (single selection) on my list view item?

How can i set selected value from alert dialog (single selection) on my list view item?...
My scenario is:
- I have listview item with button and textview
- Clicking on button in my listview item , which opens dialog box
- Selecting value from single selection list in dialog box
- Want to show this selected value from dialog in textview inside my listview item
here is my code:
Problem is: the value changed inside listitem view by setting in dialog box, doesnt persist on scroll up and down.
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
db = new DatabaseHandler(getContext());
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_event_item, parent, false);
holder = new ViewHolder();
holder.x = (TextView) convertView.findViewById(R.id.x);
holder.y = (TextView) convertView.findViewById(R.id.y);
holder.b = (Button) convertView.findViewById(R.id.b);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String s = x[position];
if (s != null){
holder.x.setText("setting text");
}
holder.b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
selected = 0;
builder.setTitle("Select ").setSingleChoiceItems(array1, selected, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selected = which;
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
value = array1[selected];
holder.y.setText(value + " ");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create();
builder.show();
}
});
return convertView;
}
I have achieved the same thing with this code
private void showDialoge(Context context, int postion) {
String[] items = {"Unpaid", " Paid "};
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setSingleChoiceItems(items, postion, null)
.setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
int selectedPosition = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
// Do something useful withe the position of the selected radio button
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

Categories

Resources