Forward compatability issues in alert dialog for Android 2.3.3
I have an issue with an Alert dialog for Android 2.3.3 HTC Desire HD.
Create list view to display items
ArrayList<HashMap<String,String>> lst = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < m_lstSettings.size(); i++)
lst.add( new HashMap<String,String>());
// While we are setting a list adapter, we are only using it to hold the
// structure of the list. We will actually override its data in getView()
// before displaying it.
setListAdapter(
new SimpleAdapter(
this,
lst,
R.layout.settings_row,
new String[] { "text1","text2" },
new int[] { android.R.id.text1, android.R.id.text2 }
) {
/**
* Override for the getView() method. This allows us to alter the display
* attributes on a line-by-line basis. We also use this opportunity to
* fill in the text fields with the right values.
*/
@Override
public View getView(int iPos, View oConvertView, ViewGroup oParent)
{
// Fetch list item based on position
ListItem oItem = m_lstSettings.get(iPos);
// create view for this row from xml layout file
View oView = m_oInflater.inflate(R.layout.settings_row, null);
// for some reason, the logic in this method call seems to be reversed,
// but it works this way
oView.setClickable(oItem.m_bLocked);
// set up the label text view
TextView tvLabel = (TextView) oView.findViewById(android.R.id.text1);
if (tvLabel != null)
{
tvLabel.setText(oItem.m_sLabel);
tvLabel.setEnabled(!oItem.m_bLocked);
}
// set up the value text view
TextView tvValue = (TextView) oView.findViewById(android.R.id.text2);
if (tvValue != null) {
tvValue.setText(oItem.m_sValue);
tvValue.setEnabled(!oItem.m_bLocked);
}
}
return oView;
}
}
);
/**
* Click handler for the list items. Will create the appropri开发者_Go百科ate edit
* box for the clicked setting.
*/
@Override
public void onListItemClick(ListView oParent, View v, int iPos, long id)
{
// inflate the xml layout
final View oView = m_oInflater.inflate(R.layout.settings_edit, null);
final ListView oParentListView = oParent;
final SettingsListItem oItem = m_lstSettings.get(iPos);
// set label
TextView tv = (TextView) oView.findViewById(R.id.TextViewEditSettings);
if (tv != null)
tv.setText(oItem.m_sLabel);
// set value
tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
if (tv != null) {
tv.setText(oItem.m_sValue);
tv.setInputType(InputType.TYPE_CLASS_TEXT);
}
// special input validator for integers
if (oItem.m_bTypeInt)
tv.setInputType(InputType.TYPE_CLASS_NUMBER);
userName = m_lstSettings.get(3).m_sValue;
// show dialog
new AlertDialog.Builder(this)
.setTitle(getString(R.string.Label_Edit) + " " + oItem.m_sLabel)
.setView(oView)
.setPositiveButton(R.string.main_connection_button_ok_button,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
TextView tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
oItem.m_sValue = tv.getText().toString();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
oParentListView.invalidate();
dialog.dismiss();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
TextView tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
dialog.dismiss();
}
})
.show();
}
XML files:
settings_row.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip"
android:background="@color/message_list_item_background"
>
<TextView android:id="@android:id/text1"
android:textSize="24sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/text2"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
settings_edit.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<TextView
android:gravity="left"
android:text=""
android:id="@+id/TextViewEditSettings"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<EditText
android:gravity="left"
android:text=""
android:id="@+id/EditTextEditSettings"
android:layout_width="fill_parent"
android:singleLine="true"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
Using the above code first we create the list view and upon clicking each item it displays an alert dialog to enter input.
We have 6 items in the list. Upon clicking the 4th item, the app crashes. It's reproducible only on the HTC Desire HD. App is working fine on other Android 2.3 devices, such as Google Nexus S and Sony Ericsson Arc.
精彩评论