如何在Android中的Hashmap中存储动态编辑文本值

时间:2012-05-16 06:48:07

标签: android hashmap

我有多个动态编辑文本框..

TableLayout ll_list = (TableLayout) findViewById(R.id.tbl);
for(i=0;i < Sizedd; i++)
{
EditText ed_comm = new EditText(this);
ll_list.addView(ed_comm);
}

如何在hashmap中存储它的值?

1 个答案:

答案 0 :(得分:2)

您可以做的是,为您创建的每个EditText设置Tag并使用Text Watcher存储他们的数据。我不擅长这个。但请尝试相应地修改我的代码段。

首先在全球声明一个HashMap,

public  HashMap<Integer,String> myList=new HashMap<Integer,String>();

并且

TableLayout ll_list = (TableLayout) findViewById(R.id.tbl);
for(i=0;i < Sizedd; i++)
{
EditText ed_comm = new EditText(this);
ed_comm.setTag(i); // By this you have set an Tag to the editText and hence you can find out which editText it is, in the TextWatcher implementation. 
ll_list.addView(ed_comm);

ed_comm.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    Log.i("After  Text","Called");
                     myList.put(ed_comm.getTag(),s.toString().trim());
                }
            });
}

就是这样。您已将值存储到hashMap。将为您在EditText中输入的每个文本调用此TextWatcher。因此,您的hashmap将随时更新。

现在从Hashmap获取数据,执行此操作,

Iterator i = myList.iterator();
            while (i.hasNext()) {
                    System.out.println(i.next());
            }
相关问题