首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >最喜欢的内容在webview上显示不正确

最喜欢的内容在webview上显示不正确
EN

Stack Overflow用户
提问于 2012-02-28 15:26:06
回答 2查看 4.1K关注 0票数 91

我正在开发一款语言词典应用。我把最喜欢的词放在首选项中。XML文件中最喜欢的内容如下所示:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
  <map>
    <string name="history">
      dict_name::160170::hi,dict_name::157140::he-man,dict_name::184774::jet,dict_name::34527::black
    </string>
    <string name="waitingTime">
      0
    </string>
    <boolean name="saveFavourite" value="true" />
    <string name="defaultDictionary">
      dict_name
    </string>
    <string name="favourite">
      dict_name::149271::go,dict_name::25481::back,dict_name::184774::jet
    </string>
    <boolean name="saveHistory" value="true" />
  </map>

我使用以下代码将喜欢的内容加载到webview中:

public class User extends Activity {
    private static final String FAVOURITE_TAG = "[MyDict - FavouriteView] ";
    private static final String CONTENT_TAG = null;

    private ListView mLSTFavourite = null;
    private ArrayList<String> lstDict = null;
    private ArrayList<Integer> lstId = null;
    private ArrayList<String> mWordFavourite = null;
    private ArrayAdapter<String> aptList = null;
    private SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favourite);
        prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        if (prefs.getBoolean("saveFavourite", true)) {
            String strFavourite = prefs.getString("favourite", "");
            Log.i(FAVOURITE_TAG, "Favourite loaded");
            if (strFavourite != null && !strFavourite.equals("")) {
                mWordFavourite = new ArrayList<String>(Arrays.asList(strFavourite.split(",")));
            } else {
                mWordFavourite = new ArrayList<String>();
            }
        } else {
            mWordFavourite = new ArrayList<String>();
        }

        Log.d(FAVOURITE_TAG, "mWordFavourite = " + mWordFavourite.size());
        mLSTFavourite = (ListView) findViewById(R.id.lstFavourite);

        ImageButton btnClear = (ImageButton) findViewById(R.id.btnClear);
        ImageButton btnBackToContent = (ImageButton) findViewById(R.id.btnBackToContent);

        if (lstDict == null) {
            lstDict = new ArrayList<String>();
            lstId = new ArrayList<Integer>();
            aptList = new ArrayAdapter<String>(getApplicationContext(), R.layout.customlist);
        }
        lstDict.clear();
        lstId.clear();
        aptList.clear();

        if (mWordFavourite != null && mWordFavourite.size() > 0) {
            try {
                for (int i = 0; i < mWordFavourite.size(); i++) {
                    Log.i(FAVOURITE_TAG, "item = " + mWordFavourite.get(i));
                    String arrPart[] = mWordFavourite.get(i).split("::");
                    if (arrPart.length == 3) {
                        Log.i(CONTENT_TAG, "loaded content " + arrPart.length + ", wordId = " + arrPart[1]);
                        // Log.i(CONTENT_TAG, "loaded 0");
                        lstDict.add(i, arrPart[0]);
                        // Log.i(CONTENT_TAG, "loaded 1");
                        lstId.add(i, Integer.parseInt(arrPart[1]));
                        // Log.i(CONTENT_TAG, "loaded 2");
                        aptList.add(arrPart[2]);
                    } else {
                        Log.i(FAVOURITE_TAG, "Wrong entry: " + mWordFavourite.get(i));
                    }
                }
            } catch (Exception ex) {
                Log.i(FAVOURITE_TAG, "Wrong entry found!");
            }
        }
        // Log.i(CONTENT_TAG,"Adapter size = " + aptList.getCount());
        // assign result return
        mLSTFavourite.setAdapter(aptList);
        mLSTFavourite.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
                        // mEDWord.setText(mAdapter.getItem(arg2));
                        Intent i = new Intent();
                        i.putExtra("dict", lstDict.get(arg2));
                        i.putExtra("wordId", lstId.get(arg2));
                        setResult(RESULT_OK, i);
                        finish();
                    }
                });
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mWordFavourite.clear();
                aptList.clear();
                mLSTFavourite.setAdapter(aptList);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("favourite", "");
                editor.commit();
                setResult(RESULT_OK);
                finish();
            }
        });

        btnBackToContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }
}

已成功加载最喜欢的内容。网页视图中列出了最喜欢的单词。但问题是,无论选择哪个单词,它都只显示添加到收藏夹中的最新单词的定义。例如:

word1
word2
word3

尽管选择了word1和/或word2作为含义,但始终会显示最后一个单词的定义,即word3。我的目的是在选择word1时显示word1的定义,在选择word2时显示word2的定义,依此类推。我将字典数据保存在SQLite数据库中。

有没有人能帮忙解决这个问题。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9477970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档