【发布时间】:2012-03-17 16:34:15
【问题描述】:
我正在开发一个语言词典应用程序。我将最喜欢的单词保存到 Preference 中。 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();
}
});
}
}
收藏内容已成功加载。最喜欢的词列在 web 视图中。但问题是它只显示添加到收藏夹中的最新单词的定义,而不管选择了什么单词。例如:
word1
word2
word3
尽管选择了 word1 和/或 word2 作为含义,但始终显示最后一个词的定义,即 word3。我的目的是在选择word1 时显示word1 的定义,在选择word2 时显示word2 的定义,以此类推。我将字典数据保存在 SQLite 数据库中。
那里的任何人都可以帮助解决这个问题。
【问题讨论】:
-
我尝试了您问题中的代码,就它而言,当在列表视图中选择其中一个时,它似乎从您的示例数据中返回了正确的字典名称和单词 ID。您提到了一个 Webview 和一个数据库,它们可能会获取并显示所选单词的数据,如果提供这些细节,可以提供更多帮助。
-
你在这里用的是什么版本的安卓? 2.3 的共享首选项有问题,我自己也遇到过,这听起来很熟悉。
-
在标题中你提到了webview,你的代码中的WebView在哪里???
-
正如@StevieB 已经提到的那样;如果没有看到 WebView 相关代码,提供更精确的帮助有点困难。我会不自觉地想到 WebView 在某些情况下缓存数据。但再说一遍:我真的需要看看你如何实现 WebView 部分才能提供更详细的帮助。
-
多么滑稽的问题!事后8个月复活,仍然没有发布相关代码,并且不知何故带有+74?这没有得到很好的研究,也没有比这个评论更有用。