Android 2.3.3 HTC Desire HD的警告对话框出现问题。
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 appropriate 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文件:
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>
首先使用上面的代码我们创建列表视图,点击每个项目后,它会显示一个警告对话框以输入输入。
列表中有6个项目。单击第4项后,应用程序崩溃。它只能在HTC Desire HD上重现。应用程序在其他Android 2.3设备上运行良好,例如Google Nexus S和Sony Ericsson Arc。