你好,游客 登录 注册 搜索
背景:
阅读新闻

Android软件开发之数据的新建 储存 读取 删除

[日期:2011-08-12] 来源:Linux公社  作者:雨松MOMO [字体:大 中 小]
3.在本地程序res/raw中读取数据操作

        Android 下提供了专门读取程序res/raw路径下资源的方法,但是没有提供写入raw内容的方法,也就是说只能读不能写,在做软件的时候有时须要读取大量的文字资源,由于这些资源文字在软件中不会改变所以无需去对它的内容重写修改,就可以使用raw来操作数据。


如图所示:在列表中读取.bin文件中的内容分别显示在listView中




        如图所示在raw路径下存了一个文件date0.bin ,下面是bin文件中保存的内容,程序中须要对这个.bin文件的内容进行读取并显示在屏幕中。




下面给出代码的实现
 
  1. public class loadRawActivity extends ListActivity {  
  2.   
  3.     private class MyListAdapter extends BaseAdapter {  
  4.     private int[] colors = new int[] { 0xff626569, 0xff4f5257 };  
  5.   
  6.     public MyListAdapter(Context context) {  
  7.         mContext = context;  
  8.     }  
  9.   
  10.     public int getCount() {  
  11.         return inpormation.length;  
  12.     }  
  13.   
  14.     @Override  
  15.     public boolean areAllItemsEnabled() {  
  16.         return false;  
  17.     }  
  18.   
  19.     public Object getItem(int position) {  
  20.         return position;  
  21.     }  
  22.   
  23.     public long getItemId(int position) {  
  24.         return position;  
  25.     }  
  26.   
  27.     public View getView(int position, View convertView, ViewGroup parent) {  
  28.         TextView tv;  
  29.         if (convertView == null) {  
  30.         tv = (TextView) LayoutInflater.from(mContext).inflate(  
  31.             android.R.layout.simple_list_item_1, parent, false);  
  32.         } else {  
  33.         tv = (TextView) convertView;  
  34.         }  
  35.         int colorPos = position % colors.length;  
  36.         tv.setBackgroundColor(colors[colorPos]);  
  37.         tv.setText(String.valueOf(position + 1) + ":"  
  38.             + inpormation[position]);  
  39.         return tv;  
  40.     }  
  41.   
  42.     private Context mContext;  
  43.     }  
  44.   
  45.     String[] inpormation = null;  
  46.     ListView listView;  
  47.   
  48.     @Override  
  49.     protected void onCreate(Bundle savedInstanceState) {  
  50.     readFile(R.raw.date0);  
  51.     setListAdapter(new MyListAdapter(this));  
  52.     listView = getListView();  
  53.     int[] colors = { 0, 0xFF505259, 0 };   
  54.     listView  
  55.         .setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));  
  56.     listView.setDividerHeight(10);  
  57.     super.onCreate(savedInstanceState);  
  58.     }  
  59.   
  60.     /** 
  61.      * 从raw中读取数据 
  62.      * @param ID 
  63.      */  
  64.     public void readFile(int ID) {  
  65.     InputStream in = null;  
  66.     String temp = "";  
  67.     try {  
  68.         in = this.getResources().openRawResource(ID);  
  69.         byte[] buff = new byte[1024];// 缓存   
  70.         int rd = 0;  
  71.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  72.         while ((rd = in.read(buff)) != -1) {  
  73.         baos.write(buff, 0, rd);  
  74.         temp = new String(baos.toByteArray(), "UTF-8");  
  75.         }  
  76.         baos.close();  
  77.         in.close();  
  78.         inpormation = temp.split("\r\n");  
  79.     } catch (Exception e) {  
  80.         Toast.makeText(this, "文件没有找到", 2000).show();  
  81.     }  
  82.     }  
  83.   
  84. }  
linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款