91

I'm developing a language dictionary app. I save the favourite word into Preference. The Favourite content in the XML file looks like the following:

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

I use the following code to load the Favourite content into the 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();
            }
        });
    }
}

The Favourite content is successfully loaded. The favourite words are listed in the webview. But the problem is that it only shows the definition of the latest word added to the Favourite regardless of what word is chosen. For example:

word1
word2
word3

Despite word1 and/or word2 is selected for meaning, the definition of the last word, which is word3, is always shown. My purpose is to display the definition of word1 when word1 is selected, and the definition of word2 when word2 is selected, and so on and so forth. I save my dictionary data in SQLite database.

Can anybody there help to solve this problem.

Khaled
  • 2,101
  • 1
  • 18
  • 26
Niamh Doyle
  • 1,909
  • 7
  • 30
  • 42
  • 18
    I tried the code in your question and as far as it goes it seems to return the correct dictionary name and word id from your sample data when one of them is selected in the listview. You mention a Webview and a database, which presumably obtain and display the data for the selected word, more help could be given if some details of these were provided. – StevieB Mar 06 '12 at 16:58
  • 6
    what version of android are you using here? 2.3 has issues with shares prefs, I've run into them myself, and this sounds like familiar effect. – nebulae Sep 26 '12 at 15:12
  • 5
    In title you mention webview, where is WebView in your code??? – Pointer Null Oct 22 '12 at 04:48
  • 2
    Just as @StevieB already mentioned; it's a bit hard to give more precise help without seeing the WebView related code. Spontaneously I can think of the WebView caching data under certain circumstances. But again: I would really need to see how you implement the WebView part to be able to give more detailed help. – dbm Oct 25 '12 at 06:56
  • 1
    agree with @hexist and dbm can you please post the layout and some piece of code for better understanding ? – AndroidLearner Oct 26 '12 at 13:39
  • 2
    What a zany question! Resurrected 8 months after the fact, still without relevant code posted, and somehow with a +74? This is not well researched, nor is it any more useful than this comment. – dokkaebi Oct 26 '12 at 18:32
  • Chances are, this will never be solved. First, you need to put things in order. Take a step back, identify all the messes and clean up the system design and the code. Chances are, this and other unidentified problems will go. – full.stack.ex Oct 28 '12 at 09:37
  • I just noticed that the "question owner" isn't the "bounty owner". Hence it might be problematic to get hold of the oh so requested WebView code. Too bad because this question really triggered my curiosity... – dbm Oct 29 '12 at 16:38

2 Answers2

3

I think the problem comes from this line

            mWordFavourite = new ArrayList<String>(Arrays.asList(strFavourite.split(",")));

where you create a new ArrayList <string> object every time but you should append to it? & get the last one otherwise you should put it in one variable

If you are checking if its size() >0

But each time you create a new object not appending to it

Maybe I am wrong!

Peter O.
  • 32,158
  • 14
  • 82
  • 96
ucefkh
  • 2,509
  • 2
  • 22
  • 18
2

http://developer.android.com/reference/android/webkit/WebView.html

please refer above link for more infomations