我正在开发新闻应用程序,在webview中显示超过50个新闻网站,用户可以打开链接并阅读新闻。我想将一些新闻保存为标题或链接,并将其显示在收藏页面中。阅读后,可以点击并删除收藏页面。
答案 0 :(得分: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
// 不要硬编码“/ sdcard” 文件sdcard = getActivity()。getFilesDir();
//获取文本文件 文件文件=新文件(sdcard,“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(); } catch(IOException e){ //你需要在这里添加适当的错误处理 }
//设置文字 tv.setText(文本[0]); Toast.makeText(getActivity(),text [0], Toast.LENGTH_SHORT).show(); } }); 返回myInflater; }
有任何意见吗?