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

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

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

下面给出代码的实现
- public class loadRawActivity extends ListActivity {
- private class MyListAdapter extends BaseAdapter {
- private int[] colors = new int[] { 0xff626569, 0xff4f5257 };
- public MyListAdapter(Context context) {
- mContext = context;
- }
- public int getCount() {
- return inpormation.length;
- }
- @Override
- public boolean areAllItemsEnabled() {
- return false;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView tv;
- if (convertView == null) {
- tv = (TextView) LayoutInflater.from(mContext).inflate(
- android.R.layout.simple_list_item_1, parent, false);
- } else {
- tv = (TextView) convertView;
- }
- int colorPos = position % colors.length;
- tv.setBackgroundColor(colors[colorPos]);
- tv.setText(String.valueOf(position + 1) + ":"
- + inpormation[position]);
- return tv;
- }
- private Context mContext;
- }
- String[] inpormation = null;
- ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- readFile(R.raw.date0);
- setListAdapter(new MyListAdapter(this));
- listView = getListView();
- int[] colors = { 0, 0xFF505259, 0 };
- listView
- .setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
- listView.setDividerHeight(10);
- super.onCreate(savedInstanceState);
- }
- /**
- * 从raw中读取数据
- * @param ID
- */
- public void readFile(int ID) {
- InputStream in = null;
- String temp = "";
- try {
- in = this.getResources().openRawResource(ID);
- byte[] buff = new byte[1024];// 缓存
- int rd = 0;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- while ((rd = in.read(buff)) != -1) {
- baos.write(buff, 0, rd);
- temp = new String(baos.toByteArray(), "UTF-8");
- }
- baos.close();
- in.close();
- inpormation = temp.split("\r\n");
- } catch (Exception e) {
- Toast.makeText(this, "文件没有找到", 2000).show();
- }
- }
- }
