I'm new to android, trying to build a simple app involving some simple data input and management. When I follow the guide here, which ask for user input through an alert dialog. Everything works fine except that, after the AlertDialog appears, the EditText
field does not have an input cursor displayed, and it won't display/update the text I typed. However, I can obtain the text string I typed in correctly after I pressed the 'OK' button in the alert dialog. Here is the xml file and code involving that dialog:
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Your Message : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/grade_in"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
code:
Button grade_in = button;
grade_in.setTag(tag);
grade_in.setOnClickListener(new OnClickListener(){
public void onClick(View btn) {
LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());
View prompt_grade_in = inflater.inflate(R.layout.grade_in, null);
final EditText input_field = (EditText)prompt_grade_in.findViewById(R.id.grade_in);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity);
alertBuilder.setView(prompt_grade_in);
alertBuilder.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.v(null, input_field.getText().toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setMessage("Grade for the course:");
AlertDialog alert = alertBuilder.create();
alert.show();
}
});
I tried adding a TextWatcher but it does not help. Is the version the source of this problem? Because I think I pretty much followed the guide except that I've made some minor changes. Thanks for helping!
p.s. the page containing the button invoke the input dialog is a fragment.
UPDATE:
The fragment containing the button consists of an ExpandableListView
, which is implemented according to this guide (part 15). And the above code implementing the alert dialog is in a custom adapter class, which create the group item in the list, inside the following function:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.textView1);
course courses = (course) getGroup(groupPosition);
((CheckedTextView) checkedTextView).setText(courses.code + " - " + courses.title);
((CheckedTextView) checkedTextView).setChecked(isExpanded);
String tag = courses.code + " - " + courses.title;
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_course);
checkbox.setChecked(sharedPref.getBoolean(tag, false));
setBoxListener(checkbox, tag);
Button button = (Button) convertView.findViewById(R.id.grade_button);
setBtnListener(button, tag);
return convertView;
}
Identified and fixed by my friend, turns out that it is a minor mistake in:
LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());
Instead it should be:
LayoutInflater inflater = LayoutInflater.from(activity);
And I thought I am using activity right from the beginning. :(