Android 2.3.3 警报对话框中的前向兼容性问题

发布于 2024-11-18 15:41:28 字数 6293 浏览 2 评论 0原文

我的 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 项。单击第四项后,应用程序崩溃。它只能在 HTC Desire HD 上重现。应用程序在其他 Android 2.3 设备上运行良好,例如 Google Nexus S 和索尼爱立信 Arc。

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 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文