Value of EditText in Custom Dialog - android

I have ListActivity , onClick of each item a Custom Dialog appears.Custom Dialog contains spinner and EditText Now i am not able to get the value of EditText, while debug value of EditText is coming as blank or "".
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
positionList=position;
HashMap<String,Object>temp =(HashMap<String ,Object>)list.get(position);
LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.answer_screen,null);
AlertDialog.Builder alertDialog=new AlertDialog.Builder(CommonScreen6.this);
alertDialog.setTitle("Select Option");
alertDialog.setView(layout);
Spinner answerList=(Spinner)layout.findViewById(R.id.spinnerAnswerList);
ArrayAdapter<String> adapterAnswerType = new ArrayAdapter<String> (CommonScreen6.this, android.R.layout.simple_spinner_item,(ArrayList<String>)temp.get(Constants.answerDesc));
adapterAnswerType.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
answerList.setAdapter(adapterAnswerType);
answerList.setOnItemSelectedListener(this);
ArrayList<String> answerDescList=(ArrayList<String>)temp.get(Constants.answerDesc);
answerList.setSelection(answerDescList.indexOf(temp.get(Constants.defaultAnswerDesc)));
alertDialog.setPositiveButton("Submit",new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.answer_screen,null);
EditText editText=(EditText)layout.findViewById(R.id.remark);
String remark=editText.getText().toString();
HashMap<String,Object>temp =(HashMap<String ,Object>)list.get(positionList);
temp.put(Constants.remark, remark);
adapter.notifyDataSetChanged();
}
});
}

The reason that you are not getting any values is because you are reinflating the layout just before you read the values, thus resetting values to the defaults.
I suppose that you would like to findViewById on the v argument that was given to the onListItemClick method, instead of reinflating the layout.
That is, instead of
LayoutInflater inflater = (LayoutInflater) CommonScreen6.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.answer_screen,null);
EditText editText=(EditText)layout.findViewById(R.id.remark);
try something like
EditText editText=(EditText)v.findViewById(R.id.remark);
You might need to make the v variable final, in order for this to work.
I hope this helps.

Related

custom AlertDialog with layout

Im trying to get the content of the editText, but idont know how to do it. Please help me in this.(Im using the method showDialog)
protected Dialog onCreateDialog(int id, Bundle args)
{
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder2.setView(inflater.inflate(R.layout.dialog_search_teacher,null));
final EditText price,city;
price=(EditText)findViewById(R.id.price_search);
city=(EditText)findViewById(R.id.city_search);
builder2.setPositiveButton("Serach", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
serach(city.getText().toString(),price.getText().toString());
}
});
builder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert2 = builder2.create();
return(alert2);}
If you need to get the text from the EditText you have to do this:
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_search_teacher, null);
city=(EditText)view.findViewById(R.id.city_search);
and then, on the onclick() method you have to retrieve the text:
city.getText().toString();
but if you want to pass the string to the parent class you have to do an interface like this:
http://developer.android.com/training/basics/fragments/communicating.html
You have to find editText id like this
price=(EditText)builder2.findViewById(R.id.price_search);
You should declaration your editText under the dialog method like,
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_search_teacher, null);
city=(EditText)view.findViewById(R.id.city_search);
After that, while doing onClick listener for postive button, you can get the content of editText using
city.getText().toString();
Hope this will help you.

Why setText() of EditText is not working?

I want to put a string in a EditText in a AlertDialog,which is in a onItemClick Listener with a ListVIew.Here is the part of code
dialogbuilder= new AlertDialog.Builder(this)
.setTitle(R.string.title_connect_dialog)
.setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.dialog_connect, null);
edit1 = (EditText)view.findViewById(R.id.devicename);
edit2 = (EditText)view.findViewById(R.id.deviceadd);
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View v, int arg2, long arg3) {
String info = ((TextView) v).getText().toString();
name = info.substring(0,info.length() - 18);
address = info.substring(info.length() - 17);
dialogbuilder.show();
edit1.setText(name);
edit2.setText(address);
}
};
When I run the project,there is nothing in the EditText!It does not work!But there is no exception or error.I tried only set text in the XML file can work.I replaced EditText with TextView ,but is not work too.
Are there something wrong in logical?Thank you for reading.
Use as view instance for accessing Views form Dialog layout which you are passing in setView method of AlertDialog.Builder change:
.setView(getLayoutInflater().inflate(R.layout.dialog_connect,null))
to
.setView(view)
EDIT:
or instead of inflate layout again to access views you should use dialogbuilder instance to initialize EditText as:
edit1 = (EditText)dialogbuilder.findViewById(R.id.devicename);
edit2 = (EditText)dialogbuilder.findViewById(R.id.deviceadd);
You are showing the Dialog before setting the text
dialogbuilder.show();
edit1.setText(name);
edit2.setText(address);
Try changing that around
edit1.setText(name);
edit2.setText(address);
dialogbuilder.show();

BadTokenException error fixed for Spinner but now TextViews not showing up in layout. How can I fix both problems?

I have a dialog with a spinner.
AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.jquery_dialog, null);
final EditText idTxt = (EditText) view.findViewById(R.id.idName);
final Spinner themeSpinner = (Spinner) view
.findViewById(R.id.themeSpinner);
final CheckBox headerChk = (CheckBox) view.findViewById(R.id.headerChk);
final CheckBox footerChk = (CheckBox) view.findViewById(R.id.footerChk);
final RadioGroup group = (RadioGroup) view
.findViewById(R.id.jqmNavigation);
customDialog.setView(idTxt);
customDialog.setView(headerChk);
customDialog.setView(footerChk);
themeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
themes = getResources().getStringArray(R.array.themes);
theme = themes[arg2];
Toast.makeText(NewFile.this, theme, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
When I click the Spinner I get the error
04-25 18:01:56.299: E/AndroidRuntime(21639331): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
I fixed this problem by changing the LayoutInflater code to
LayoutInflater layoutInflater = LayoutInflater.from(MyActivity.this);
which fixes my Spinner error but now none of the TextViews in my Dialog show up. How Can I fix both problems?
EDIT
public void jqueryMobileDialog() {
AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = context.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.jquery_dialog, null);
final EditText idTxt = (EditText) view.findViewById(R.id.idName);
final Spinner themeSpinner = (Spinner) view
.findViewById(R.id.themeSpinner);
final CheckBox headerChk = (CheckBox) view.findViewById(R.id.headerChk);
final CheckBox footerChk = (CheckBox) view.findViewById(R.id.footerChk);
final RadioGroup group = (RadioGroup) view
.findViewById(R.id.jqmNavigation);
customDialog.setView(idTxt);
customDialog.setView(headerChk);
customDialog.setView(footerChk);
themeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
themes = getResources().getStringArray(R.array.themes);
theme = themes[arg2];
Toast.makeText(NewFile.this, theme, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The layout inflater needs to come from a context with the right to draw to the screen. Only the current activity has this. SO calling getApplicationContext().getLayoutInflater will never work- the application context doesn't have permission. Use the getLayoutInflater of your current activity instead.
There are two other times this can happen. One is if you're popping up a dialog too quickly (before your activity has the right to display). The only solution I've found for this is to send a message to yourself to retry in 100 ms when that happens, and retry a couple of times. This will happen if, for example, you try to show your dialog in onCreate.
The other is when your dialog tries to show at the same time your activity loses the foreground. There is no solution for this, because you don't know if/when you're going to become the foreground app again. Here you want to catch and ignore. Note that this may also happen around rotation and other config changes that terminate and restart the activity.

Not able to get input from the custom fragment dialog

i am displaying a custom dialog for user input on top of my list view activity layout. But when i try to refer the edit text in onCreateDialog() method, its always coming as blank. Below is my code, appreciate if someone can point out where i am doing wrong -
public class QuestionDialog extends DialogFragment
{
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(inflater.inflate(R.layout.ques_dialog, null))
.setTitle("Title Message!")
.setCancelable(true)
.setPositiveButton("GO", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
EditText et1 = (EditText)v.findViewById(R.id.et1);
// here not able to get the data from edit text
String s1 = et1.getText().toString().trim();
Log.d("debug","data -"+s1+" and length"+s1.length());
.... rest code goes here
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
..rest code goes here
}
});
return builder.create();
}
}
That is happening because of this lines:
final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(inflater.inflate(R.layout.ques_dialog, null))
You first inflate the v View and then set as the content of the dialog a newly inflated view(so in the end you end up looking for a view which isn't the actual content of the dialog). Use this:
final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(v);

Custom listview for AlertDialog

I'm trying to have my AlertDialog with a custom list view but can't seem to get it to show or run without error.
private void buildDialog(){
int selectedItem = -1; //somehow get your previously selected choice
LayoutInflater inflater = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.listview, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
builder.setTitle("Select Weapon").setCancelable(true);
builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
ListView lv = ((AlertDialog) dialog).getListView();
itemId = lv.getAdapter().getItemId(which);
new changeEQ().execute();
}
});
dialog = builder.create();
}
This is my AlertDialog but can't figure out what to add to get my custom layouts, listview & listrow to be used. I've looked around at guides online but nothing they show seems to work for me. IE I must be doing something wrong.
EDIT: changed code to include answer but has no change on what is showed on screen. No errors yet no change in look.
If you have a custom layout that you want to pass to your AlertDialog try:
LayoutInflater inflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.custom_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
If you want to define listeners try:
ListView list = (ListView) customView.findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do as you please
}
});

Categories

Resources