0

我不知道为什么我的代码不会显示在我的编辑文本上,有人可以帮忙吗?我真的需要这个工作。

我在 res\raw 文件夹下创建了我的 xml 作为菜单。我不知道读取文件是否有问题。我希望edittext在运行时/oncreate时显示

public class CreateTreasureActivity extends Activity {

    private EditText      name;
    private EditText      clue1;
    private EditText      clue2;
    private EditText      clue3;
    private EditText      answer;
    private EditText      location;
    private EditText      point_value;

    FileOutputStream myFile;
    XmlSerializer serializer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_treasure);


        name       = (EditText) findViewById(R.id.name);          
        name.setText("");

        clue1       = (EditText) findViewById(R.id.clue1);          
        clue1.setText("");

        clue2       = (EditText) findViewById(R.id.clue2);          
        clue2.setText("");

        clue3       = (EditText) findViewById(R.id.clue3);          
        clue3.setText("");

        answer       = (EditText) findViewById(R.id.answer);          
        answer.setText("");

        location       = (EditText) findViewById(R.id.location);          
        location.setText("");

        point_value       = (EditText) findViewById(R.id.point_value );          
        point_value .setText("");
        // create the initial xml document if it already does not exist
        try 
        {
            File filename = new File(getFilesDir(), "treasure.xml");

            if(!filename.exists())
            {
                myFile = openFileOutput("treasure.xml", Activity.MODE_APPEND);

                // create a new XmlSerializer object 
                serializer = Xml.newSerializer();

                // use myFile as your xml serializer and set to UTF-8 encoding
                serializer.setOutput(myFile, "UTF-8");

                // Write <?xml declaration with encoding 
                serializer.startDocument(null, Boolean.valueOf(true));

                // set indentation option 
                serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

                // start a new top level tag and other tags
                serializer.startTag(null, "Treasure");
            }
            else
            {
                // use myFile as your xml serializer and set to UTF-8 encoding
                myFile = openFileOutput("treasure.xml", Activity.MODE_APPEND);

                // create a new XmlSerializer object 
                serializer = Xml.newSerializer();

                // use myFile as your xml serializer and set to UTF-8 encoding
                serializer.setOutput(myFile, "UTF-8");
            }
        }
        catch (Exception e)
        {
            Log.e("Exception::",  e.getMessage());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.create_treasure, menu);
        return true;
    }

    public void onSaveButton(View v)
    {
        try
        {   
            // start a new top level tag and other tags, for our FINAL project, Contact could be Treasure Item
            // we don't need to have nested <Treasure> followed by Items, the format can be similar to the below
            // this way, we don't need to add an end tag for outermost element

            // create tags and values
            serializer.startTag(null, "Items");
            serializer.startTag(null, "treasure");
            serializer.startTag(null, "name");
            serializer.text(name.getText().toString());  // retrieve name entered by user from text field
            serializer.endTag(null, "name");

            serializer.startTag(null, "clue1");
            serializer.text(clue1.getText().toString());
            serializer.endTag(null, "clue1");

            serializer.startTag(null, "clue2");
            serializer.text(clue2.getText().toString());
            serializer.endTag(null, "clue2");

            serializer.startTag(null, "clue3");
            serializer.text(clue3.getText().toString());
            serializer.endTag(null, "clue3");

            serializer.startTag(null, "answer");
            serializer.text(answer.getText().toString());
            serializer.endTag(null, "answer");


            serializer.startTag(null, "pointValue");
            serializer.text(point_value.getText().toString());
            serializer.endTag(null, "pointValue");
            serializer.endTag(null, "Treasure");
            serializer.endTag(null, "Items");

            // perform the write by flushing
            serializer.flush();

            // clear the text fields
            name.setText("");
            clue1.setText("");
            clue2.setText("");
            clue3.setText("");
            answer.setText("");
            location.setText("");
            point_value.setText("");
        } 
        catch (Exception e)
        {
            // use logcat to display errors
            Log.e("Exception::",  e.getMessage());
        }       
    }
}
4

1 回答 1

0

我在这里看到的是将用户写入的 EditText 中的数据保存到您的 Treasure.xml 文件中的代码。这可能还行不通,因为你的

 public void onSaveButton(View v)

无处可称。

当您要将用户写入的数据保存到存储时,应该调用它。因此,您可以添加另一个 Button 调用 onSaveButton(); 添加 OnCreate(...) 方法的末尾。您也可以将参数 View v 更改为空的,因为我在这里看不到它的必要性:

Button saveButton = new Button(this);
saveButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        onSaveButton();
    }
});

如果要从 Treasure.xml 中读取数据,请使用 File* Input *s,而不是 Outputs:

FileInputStream in = openFileInput("treasure.xml");
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
    sb.append(line);
}

在 while() {...} 循环中,您已将 Treasure.xml 中的每一行读入一个名为 line 的字符串。所以把你想用这条线做什么。例如,您可以等待查看字符串是否为“”,然后您知道下一行将是您的 EditText 名称的输入,直到一行为“”,因此您可以使用。

如果您只是想知道如何将数据从应用程序保存/加载到存储中并且只是认为您可以简单地使用 xml 文件来执行此操作。您最好查看http://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage,了解如何保存/加载到内部/外部存储。(您的 Treasure.xml 文件仅在 eclipse 的项目目录下,不在您的真实设备上)。

于 2013-08-28T00:33:08.030 回答