2012-05-16 43 views
-3

我有多個動態的EditText框..如何存儲動態編輯的文本價值的Hashmap在android系統

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中值?

+0

使用的EditText作爲鍵和文本值的id? – L7ColWinters

+0

不要創建重複的問題。您已經在2個小時前提出過這個問題。 –

+1

使用HashMap創建散列表 map_name = new HashMap ();並且使用'map_name.put(a_string,another_string)來放置值;'使用'edittext_id.getText()'獲取值從** EditText **。 –

回答

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()); 
       } 
      }); 
} 

就是這樣。您已將值存儲到一個哈希映射。這個TextWatcher會被輸入到你的EditText中的每個文本都被調用。所以你的hashmap將隨時更新。

現在來從HashMap中的數據,做到這一點,,

Iterator i = myList.iterator(); 
      while (i.hasNext()) { 
        System.out.println(i.next()); 
      } 
+0

我有按鈕,當我點擊按鈕其調用system.out.Println(myList);其顯示出來{2 = zxc} – user1153176

+0

是的。你只能在第二個編輯文本中輸入文本。嘗試編輯所有的EditText並檢查 –

相關問題