【问题标题】:How to get and store multiple genrated edittext value in android?如何在android中获取和存储多个生成的edittext值?
【发布时间】:2015-12-15 15:15:20
【问题描述】:

我在单击按钮时添加多个 Edittext。我也得到了这些 Edittext 的值,但我无法将数据存储在数组中。

EditText textIn;
Button buttonAdd, buttonShow;
LinearLayout container;
List<EditText> allEds = new ArrayList<EditText>();

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

    buttonAdd = (Button)findViewById(R.id.add);
    buttonShow = (Button) findViewById(R.id.show);
    container = (LinearLayout)findViewById(R.id.container);

    buttonAdd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            LayoutInflater layoutInflater =
                    (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.row, null);
            EditText editText1 = (EditText) addView.findViewById(R.id.editText1);
            EditText editText2 = (EditText) addView.findViewById(R.id.editText2);
            allEds.add(editText1);
            allEds.add(editText2);
            Button buttonRemove = (Button) addView.findViewById(R.id.remove);
            buttonRemove.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((LinearLayout) addView.getParent()).removeView(addView);
                }
            });

            container.addView(addView);
        }
    });

    buttonShow.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            String[] strings = new String[allEds.size()];

            for(int i=0; i < allEds.size(); i++){
                strings[i] = allEds.get(i).getText().toString();
                Log.e("My data", strings[i]);
            }
        }
    });

在这里,我使用 strings[i] 获取所有值,但我想像这样将值存储在数组中。 [{"Name": "Smith","Age", "26"},{"Name": "Jhon","Age", "30"}]。在这里,我将从多个生成的 Edittext 中得到 Smith,26 和 Jhon,30。生成更多动态字段后,将扩展数组。 请帮我。

【问题讨论】:

  • 你想要它作为Json字符串[{"Name": "Smith","Age": 26}]吗?或 Array 中键为 Name、Age 且值为 Smith 和 26 的元素?

标签: java android arrays dictionary dynamic


【解决方案1】:

你说你必须把它作为一个数组来获取。如我所见,您已经将它作为 String 数组。你还想要什么?您描述预期结果的方式看起来像半生不熟的 json 输出。或许您可以创建一个代表您想要其信息的 Person 的类,然后创建一个 Person 类型的 ArrayList。此外,为了使 Person 对象中的内容更容易填充,您可以声明一个以 nameage 作为参数的构造函数。在您的循环中,您可以使用该构造函数创建一个 Person 对象,然后将该对象添加到 ArrayList
这会有所帮助:

class Person{
        String name;
        int age;
        Person(String name, int age){
            this.name=name;
            this.age=age;
        }
    }

然后在进入循环之前,声明一个 Person 类型的 ArrayList

ArrayList<Person>persons=new ArrayList<Person>();

最后在你的循环中:

Person temp = new Person(*get the name, get the age*);

persons.add(temp);

瞧!你已经准备好你的 person ArrayList 了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    相关资源
    最近更新 更多