存储为以后的工作用户输入数据的最佳方式 [英] The best way to store user input data for later work

查看:110
本文介绍了存储为以后的工作用户输入数据的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发,我将AP preciate任何帮助,我可以得到的。

I'm new to Android development and I will appreciate any help I can get.

我设计,在某些时候需要询问用户他的朋友的名字,以便在以后与这些名字的工作的应用程序,即这些名字将在下拉列表中使用,将在一个单独的显示查看。

I'm designing an app that at some point needs to ask user for his Friends' names in order to work with those names later on, i.e those names will be used in drop-down lists and will be displayed at a separate View.

我的问题是:什么是有效地存储这些名字,然后能够获得访问它们的阅读,编辑和删除的最佳方式?名字的量不会大(最多20个项目)。

My question is: what is the best way to efficiently store those names and then be able to get access to them for reading, editing and deleting? The amount of names will not be big (at most 20 items).

在回应有关添加更多信息注释:

In response to the comment about adding more info:

我需要一个用户来指定将在2个不同的Andr​​oid活动中使用的名称(字符串)的列表:
1)这名单将在一个微调是申请表的一部分被用于
2)这名单将在专为操纵(编辑和删除)一个独立的活动中使用现有的项目,并添加新的。

I need a user to specify list of names (strings) that will be used in 2 different Android Activities: 1) This list of names will be used in a Spinner that is a part of an application form 2) This list of names will be used on a separate Activity designed for Manipulating (Editing and Deleting) of existing items and adding new ones.

我还需要一个操作(编辑,删除和创建新的项目)与此列表后的变化发生在这两个活动。用户退出应用程序后,所以我的理解是应该的地方存放在内部存储此列表应该是可用的。

I also need that after manipulations (editing, deleting and creating new items) with this list changes took place in Both Activities. This list should be available after user exits the app, so as I understand it should be stored somewhere in Internal Storage.

推荐答案

如果你打算只存储20个项目,也许是最好的办法是写入和读取的文件。

If you are going to store only 20 items, maybe the best way is to write and read a file.

public void writeItems(String fileName) {
  final String ls = System.getProperty("line.separator");
  BufferedWriter writer = null;
  try {
    writer = 
      new BufferedWriter(new OutputStreamWriter(openFileOutput(fileName, 
        Context.MODE_PRIVATE)));
    writer.write("Item 1" + ls);
    writer.write("Item 2" + ls);
  } catch (Exception e) {
      e.printStackTrace();
  } finally {
    if (writer != null) {
    try {
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    }
  }
}


public void readItems(String fileName) {

  BufferedReader br = null;
  try {
    br = new BufferedReader(new InputStreamReader(openFileInput(fileName)));
    String line;

    while ((line = input.readLine()) != null) {
     //do something
    }
  } catch (Exception e) {
     e.printStackTrace();
  } finally {
  if (input != null) {
    try {
    br.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    }
  }
} 


openFileInput openFileOutput 引用:<一href=\"http://developer.android.com/reference/android/content/Context.html#openFileInput%28java.lang.String%29\" rel=\"nofollow\">http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)

这篇关于存储为以后的工作用户输入数据的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆