2012-02-28 33 views
91

Tôi đang phát triển một ứng dụng từ điển ngôn ngữ. Tôi lưu từ yêu thích vào Preference. Nội dung yêu thích trong file XML trông giống như sau:Nội dung yêu thích không được hiển thị chính xác trên chế độ xem web

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

tôi sử dụng đoạn mã sau để tải các nội dung yêu thích vào 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(); 
      } 
     }); 
    } 
} 

nội dung yêu thích được nạp thành công. Các từ yêu thích được liệt kê trong chế độ xem web. Nhưng vấn đề là nó chỉ hiển thị định nghĩa của từ mới nhất được thêm vào Favorite bất kể từ nào được chọn. Ví dụ:

word1 
word2 
word3 

Mặc dù word1 và/hoặc word2 được chọn cho ý nghĩa, định nghĩa của từ cuối cùng, là từ3, luôn được hiển thị. Mục đích của tôi là hiển thị định nghĩa của word1 khi chọn word1 và định nghĩa của word2 khi word2 được chọn, v.v. Tôi lưu dữ liệu từ điển của tôi trong cơ sở dữ liệu SQLite.

Ai đó có thể giúp giải quyết vấn đề này.

+18

Tôi đã thử các mã trong câu hỏi của bạn và như xa như nó đi có vẻ như để trả lại đúng từ điển tên và chữ id từ dữ liệu mẫu của bạn khi một trong chúng được chọn trong listview. Bạn đề cập đến một Webview và một cơ sở dữ liệu, mà có lẽ có được và hiển thị dữ liệu cho từ đã chọn, có thể có thêm trợ giúp nếu một số chi tiết được cung cấp. – StevieB

+6

bạn đang sử dụng phiên bản Android nào? 2.3 có vấn đề với prefs cổ phiếu, tôi đã chạy vào chúng bản thân mình, và điều này nghe có vẻ như hiệu ứng quen thuộc. – nebulae

+5

Trong tiêu đề bạn đề cập đến webview, đâu là WebView trong mã của bạn ??? –

Trả lời

3

Tôi nghĩ rằng vấn đề xuất phát từ dòng này

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

nơi bạn tạo một đối tượng ArrayList <string> mới mỗi thời gian nhưng bạn nên thêm cho nó? & được người cuối cùng nếu không bạn nên đặt nó trong một biến

Nếu bạn đang kiểm tra nếu kích thước của nó()> 0

Nhưng mỗi khi bạn tạo một đối tượng mới không phụ thêm để nó

lẽ Tôi sai!

2

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

vui lòng tham khảo trên liên kết để biết thêm THÔNG TIN CẦN BIẾT

+0

Chào mừng bạn đến với StackOverflow! Bạn có muốn thêm một đoạn trích của bài viết vào câu trả lời của bạn, để nó có thể tự đứng trên đó không? –

Các vấn đề liên quan