2015-10-10 34 views
-1

我開發新聞類應用,其顯示在網頁流量和用戶超過50個新聞網站可以打開鏈接和閱讀新聞。我想保存一些新聞作爲標題或鏈接,並將它們顯示在最喜歡的頁面中。並且在收藏頁面可以在閱讀後點擊並刪除。添加web視圖鏈接到收藏夾

回答

0

我的工作的想法與長按頁寫鏈接到一個文件,然後讀取該文件,並在收藏夾列表。我用文本視圖進行測試,寫入文件似乎沒問題,但是讀取它卻沒有。我的代碼是:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View myInflater= inflater.inflate(R.layout.fragment_main, container, false); 

    TextView txtView = (TextView) myInflater.findViewById(R.id.txtView); 
    txtView.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getActivity(), 
        "You have pressed it long :)", Toast.LENGTH_LONG).show(); 

      String filename ="favoritessss.txt"; 
      File file = new File(getActivity().getFilesDir(), filename); 
      File directory = getActivity().getDir("SunShine", Context.MODE_WORLD_READABLE); 

      //String filename = "myfile"; 
      String string = "Hello world000000000!"+"\r\n"; 
      FileOutputStream outputStream; 

      try { 
       outputStream = getActivity().openFileOutput(filename, Context.MODE_WORLD_WRITEABLE); 
       outputStream.write(string.getBytes()); 
       outputStream.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
     } 
     return true; 
    } 
}); 

    //Find the view by its id 
    final TextView tv = (TextView)myInflater.findViewById(R.id.text_view); 

    txtView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      //Find the directory for the SD Card using the API 

// 不要硬編碼 「/ SD卡」 文件的SD卡= getActivity()getFilesDir()。

//獲取文本文件 檔案文件=新的文件(SD卡, 「favoritessss.txt」); String [] text = new String [15]; 嘗試BufferedReader br = new BufferedReader(new FileReader(file)); for(int j = 0; j> 15; j ++)text [j] = br.readLine(); br.close(); } 趕上(IOException異常E){// 你需要添加合適的錯誤處理 }

//設置文本 tv.setText(文[0]); Toast.makeText(getActivity(),文本[0], Toast.LENGTH_SHORT).show(); } }); return myInflater; }

有何評論?