Android 安卓可以';动态设置视图后,无法按id检索视图
我使用的是ListFragment,在这个片段和onCreate函数中,我使用的是自定义游标适配器,它会重复我的布局若干次, 布局包含一些元素,其中一个是EditText,我需要在后面处理这些EditText。 在这个适配器中,我动态地更改EditText的id 这样Android 安卓可以';动态设置视图后,无法按id检索视图,android,android-edittext,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.editTe
//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保存在SharedReference中,因此在下面的代码中,我从SharedReference中检索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 ");
}
那么问题出在哪里呢???也发布检索代码。同样,提供logcat将有助于快速解决您的问题。我添加了详细的代码,logcat和我正在等待答案。这可能有助于您在上述代码中充气
查看myLayout
,myLayout获得layout.xml中提到的分配id。因此,请按照上面提供的链接来解决问题谢谢,它起作用了谢谢它起作用了:)但现在我有另一个问题,当我在同一版面中有多个editText时,我无法真正关注editText。每当我把光标放在编辑文本中,然后移动到键盘上,焦点就从编辑文本开始!!!
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