android动态设置后无法通过id检索视图

时间:2013-05-07 08:58:14

标签: android android-edittext

我正在使用ListFragment,在这个片段和onCreate函数中,我使用自定义光标适配器,重复我的布局一定次数, 布局包含一些元素,其中一个是EditText,我需要处理后面的这些EditText。  在这个适配器里面,我正在动态地改变EditText的id 像这样

//get EditText and give it the same id of dish 
     EditText  noteEditText = (EditText) v.findViewById(R.id.editTextNote);
     if (noteEditText != null){
            int record_ID_int = Integer.parseInt(record_ID);
            noteEditText.setId(record_ID_int);
            noteEditText.setText(""+record_ID_int);

                        EditText tempEditText = (EditText) v.findViewById(record_ID_int);
            if (tempEditText != null){
                tempEditText.setText(""+record_ID_int);
                 Log.e("layout"," tempEditText is Not Not null after retriveing it by id :"+record_ID_int);
            }else{
                   Log.e("layout"," tempEditText is  null null null   by id  :"+record_ID_int);
            }           
        }

其中editTextNote是布局中设置的静态ID 正如你所看到的,我正在执行另一个操作,除了更改id,我也设置文本,所以我可以肯定操作进展顺利,当我运行应用程序时,我可以看到文本设置正确。  在这个游标适配器块中,layout(v)能够通过在刚刚传递的id上调用findViewById来获取EditText

但是当我试图通过向它传递相同的id来检索这些EditText时,它返回null!我在函数onStart()中将检索EditText的代码放在同一个片段中。 注意::我保存了我在SharedPreference中设置为EditText的id,所以在下面的代码中我从SharedPreference中检索id,然后将它们传递给函数findViewById以获取所需的EditText 检索editText组件的代码是:

  LayoutInflater inflater = getActivity(). getLayoutInflater();
   View myLayout =inflater.inflate(R.layout.display_order_list_layout, null, false);       
   View parentView =(View) myLayout.getRootView();

   if (myLayout != null){
       //1::get sharedPrefNotes from my classes 
        ManageOrder manageOrder = new ManageOrder(getActivity());
         int []  keysNotesArray = manageOrder.getAllSharedPreferenceNoteKeys();

        //below just a test to see it the layout can get other per-definned id's elements in layout 
         TextView  textView = (TextView) myLayout.findViewById(R.id.textViewTotalPriceThisDishStr);
         if (textView != null ){
             Log.e("layout","textView with id : textViewTotalPriceThisDishStr| text is : "+textView.getText()); 
         }else {
               Log.e("layout","textView is null ,id  textViewTotalPriceThisDishStr");
         }


         for (int i = 0 ; i<keysNotesArray.length; i++){

             int key_id = keysNotesArray[i];
             //2::find item by id 
             EditText  noteEditText = (EditText) myLayout.findViewById(key_id);
             if (noteEditText != null ){
                 Log.e("layout","EditText with id :"+key_id +"| text is : "+noteEditText.getText()); 
             }else {
                   Log.e("layout","EditText is null od id  "+key_id);
             }


         }

   }else {
       Log.e("layout","myLayout is null ");
   }

日志文件显示以下结果

05-07 19:41:25.584: E/layout(5002): 114: 
05-07 19:41:25.584: E/layout(5002): 95: 
05-07 19:41:25.584: E/layout(5002): 75: 
05-07 19:41:25.584: E/layout(5002): 123: 
05-07 19:41:25.585: E/layout(5002): textView  with id : textViewTotalPriceThisDishStr| text is : الاجمالي
05-07 19:41:25.585: E/layout(5002): EditText is null , id  114
05-07 19:41:25.586: E/layout(5002): EditText is null , id  95
05-07 19:41:25.586: E/layout(5002): EditText is null , id  75
05-07 19:41:25.586: E/layout(5002): EditText is null , id  123

那么问题是什么?

0 个答案:

没有答案