0

我正在使用 ListFragment ,在这个片段内部和 onCreate 函数中我正在使用自定义光标适配器,它会重复我的布局一定次数,布局包含一些元素,其中一个是 EditText,我需要处理这些EditText(s) 后者。在这个适配器中,我正在像这样动态更改 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 之外,我正在执行另一个操作,我也在设置文本,所以我可以确定操作进展顺利,当我运行应用程序,我可以看到文本设置正确。在这个光标适配器块内,布局 (v) 能够通过调用刚刚传递的 id 上的 findViewById 来获取 EditText

但是当我试图通过将相同的 id 传递给它来检索这些 EditText 时,它返回 null !我将检索 EditText 的代码放在同一个片段中,在函数 onStart() 中。注意::我将设置为 EditText 的 ID 保存在 SharedPreference 中,因此在下面的代码中,我从 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

那么问题是什么?

4

0 回答 0