数据存储与检索


      package com.msi.manning.chapter5.filestorage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParser;

public class ReadXMLResourceFile extends Activity {

    private static final String LOGTAG = "FileStorage";

    private TextView readOutput;
    private TextView testView;
    private TextView testName;
    private Button gotoReadWriteSDCard;

    @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.read_xmlresource_file);

        this.readOutput = (TextView) findViewById(R.id.readxmlres_output);
        this.testView = (TextView) findViewById(R.id.test);
        this.testName = (TextView) findViewById(R.id.testName);
        XmlPullParser parser = getResources().getXml(R.xml.people);
        StringBuffer sb = new StringBuffer();
        StringBuffer test = new StringBuffer();
        StringBuffer testN=new StringBuffer();;
        try {
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
                String name = parser.getName();
                Log.v(ReadXMLResourceFile.LOGTAG, "    parser NAME - " + name);
                String first = null;
                String last = null;
                testN.append("  // name:"+name);
               
                if ((name != null) && name.equals("person")) {
                    int size = parser.getAttributeCount();
                    for (int i = 0; i < size; i++) {
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        test.append("  size: "+size+"  attrName: "+attrName+"  attrValue:  "+attrValue+"/n");
                       
                        if ((attrName != null) && attrName.equals("firstname")) {
                            first = attrValue;
                        } else if ((attrName != null) && attrName.equals("lastname")) {
                            last = attrValue;
                        }
                    }
                    if ((first != null) && (last != null)) {
                        sb.append(last + ", " + first + "/n");
                    }
                }
            }
            this.testName.setText(testN.toString());
            this.testView.setText(test.toString());
            this.readOutput.setText(sb.toString());
        } catch (Exception e) {
            Log.e(ReadXMLResourceFile.LOGTAG, e.getMessage(), e);
        }       

        this.gotoReadWriteSDCard = (Button) findViewById(R.id.readwritesdcard_button);
        this.gotoReadWriteSDCard.setOnClickListener(new OnClickListener() {
            public void onClick(final View v) {
                startActivity(new Intent(ReadXMLResourceFile.this, ReadWriteSDCardFile.class));
            }
        });

    }
}
 

为了弄明白其中的getName(),getAttributeCount()(开始自己以为是4,即是指四个person),getAttributeName(),getAttributeValue()()各自究竟是指什么,就在程序中添加了2个StringBUffer(TestN,TestA),和两个TextView,分别用来测试getName,与(),getAttributeCount,getAttributeName(),getAttributeValue()()。使用testN.append("  // name:"+name);与testA.append("  size: "+size+"  attrName: "+attrName+"  attrValue:  "+attrValue+"/n");来分别吧每次循环加到字符串的末尾,最后this.testName.setText(testN.toString());
            this.testView.setText(testA.toString());

结果如下

发现原来getAttributeCount是2,再看看输出的attributeName与attributeValue就明白了原来他是指每个person中的属性的个数在这里也就是firstName与lastName,即如下的people.xml文件所示:<people>
 <person firstname="John" lastname="Ford" />
 <person firstname="Alfred" lastname="Hitchcock" />
 <person firstname="Stanley" lastname="Kubrick" />
 <person firstname="Wes" lastname="Anderson" />
</people>
 

这个问题就很好解决了,

但是第一个textView的输出,即在测试大的while循环中测试getName得到的情况很让我不解,既然第二个测试getAttribute中的情况是指每一行的,那个在这个大的while循环里他应该是循环四次,那按常理应该是输出四个person呀,为什么1:先输出了一个Null ,为什么2 :把people读取了(好吧,如果他按顺序读取需要把它读取,这确实对下一个textView的输出无影响,因为源程序中在进入for循环前有这个判断,算是解决了,就当是知道原来people首尾也有读了,不过第一个NULL仍是不解):

if ((name != null) && name.equals("person")) {
                    int size = parser.getAttributeCount();
                    for (int i = 0; i < size; i++) {
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        testA.append("  size: "+size+"  attrName: "+attrName+"  attrValue:  "+attrValue+"/n");
                       
                        if ((attrName != null) && attrName.equals("firstname")) {
                            first = attrValue;
                        } else if ((attrName != null) && attrName.equals("lastname")) {
                            last = attrValue;
                        }
                    }
                    if ((first != null) && (last != null)) {
                        sb.append(last + ", " + first + "/n");
                    }
                }

为什么3:他怎么输出了8个person????(不是一次while循环中他是把一行即firstName与lastName读取了吗,怎么不是4个person呢?)

另外同时将数据的存储与检索方式总结一下:

一:使用首选项sharePreferences:

可以通过当前的context来访问sharePreferences对象:使用getSharePreferences(string name,int accessMode)方法就可以获得一个首选项句柄。name表示相关首选项的的文件,即当调用此方法时,若系统中有以该字符创作为文件名的优先级设置文件,如没有,则会使用该名字自动创建一个文件,accessMode是指你希望指定的权限,即是你允许其他application对他是可读或可写什么的吧,我看那些常量的定义好似这样的。例如:

2 使用SharePreference的edit()方法获取其编辑器。

3 使用编辑器的putString(string key,string value)存储数据

4 使用编辑器引用变量的commit()方法来提交更改。因为使用Editor存储数据后,他将在内存中创建一个map,必须调用commit()方法来将数据持久地保存到首选项支持的文件中。

在同一个包中要获取sharePreferences对象中的数据,只需要再次声明sharePreferences变量并且指定引用,就可以使用getSring(steing key,string value)来获取数据。

附上此用法例子一个:

第一个activity代码:

第二个activity代码:访问了前一个的数据:

第一个Layout

第二个Layout:

试验结果:

 

 

当要在一个包中获取另外一个包(另一个应用程序)的数据时,最难的是找到其路径,而该路径是通过context创建的,所以我们需要使用createPackgeContext(string context_name,string Mode)来获取其他应用程序的Context人,然后直接引用其他应用程序中的sharePreference对象的名称(我们必须知道他们的名称)来访问这些首选项。

例如在另外一应用程序中要访问上面的应用程序的数据:

Java代码:

Layout代码:

  

 结果如下:其中“Ican't access to them ”是因为权限问题无法访问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值