3

无论如何可以告诉我为什么要跳过以下 if 语句?我想检查我的 mAlphabetCode 是否包含 0-9 文字,而不是 0 到 9。

        // check if alphabet code is numeric
        if (mAlphabetCode.equals("0-9")){
            mAlphabetCode = "-";
        }

这是整个代码:

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    if (parent.getId() == R.id.lv_gl_content) {
        // store data to be pass
        Intent mIntent = new Intent(this, AchList.class);
        mIntent.putExtra("mPageUrl", mGameList.get(position).getItemPageUrl());
        mIntent.putExtra("mGameTitle", mGameList.get(position).getTitle());

        // start activity and page animation
        startActivity(mIntent);
        mPageTrans.slideIn();

    } else if (parent.getId() == R.id.lv_alphabet_content) {
        // get alphabet code
        String mAlphabetCode = parent.getAdapter().getItem(position).toString().toLowerCase();

        // check if alphabet code is numeric
        if (mAlphabetCode.equals("0-9")){
            mAlphabetCode = "-";
        }

        // build page url
        mGameListUrl = mGameListUrl.substring(0, (mGameListUrl.length() - 2) ) + mAlphabetCode + "/";

        mAlphabetMenu.setItemChecked(position, true);

        // close browsing menu
        mSlidingPane.closePane();

        // make network request
        mStringRequest = new StringRequest(Request.Method.GET, mGameListUrl, onSuccess(), onError());
        mRequestQueue.add(mStringRequest);
    }
}

这是调试器在点击 if 语句之前所说的 mAlphabetCode 包含的内容:

在此处输入图像描述

我的错误出现在我的 strings.xml 文件中:

    <!-- strings of arrays -->
<string-array name="slide_menu_alphabet">
    <item>0&#8211;9</item>

我已将项目从 0-9 更改为 0 – ;9 (间隔数字显示),因为 AndroidStudio IDE 受制于 @user1873880 和 David Cesarino,我更改为 0 - ;9 (间隔数字显示)现在效果很好。

谢谢您的帮助。

4

1 回答 1

6

问题是代码中的“-”字符实际上是整数 45,而在调试器中它是 8211,这意味着它是不同的字符。您可以通过一些日志来验证这一点,只需尝试查看此值即可(int) '-'

于 2013-07-23T23:16:29.983 回答