0

我有一个带子LinearLayout的ScrollView。我通过一个单独的View方法以编程方式将TextViews和NumberPickers添加到LinearLayout。但是,当单击包含ScrollView的选项卡时,动态对象不会显示。不显示在片段中的动态视图:android

这里是我的代码:

@Override 
 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 

 
    View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false); 
 
    super.onCreate(savedInstanceState); 
 
    myDb = new DatabaseHelper(getContext()); 
 

 
    rootview.findViewById(R.id.scroll_config); 
 
    viewFunds(); 
 
    return rootview; 
 
}

这里是单独的方法我已经提到,动态地添加对象到的LinearLayout:

public View viewFunds(View rootview) { 
 

 
    ScrollView scrollView = new ScrollView(getActivity()); 
 
    scrollView.findViewById(R.id.scroll_config); 
 
    LinearLayout linearLayout = new LinearLayout(getActivity()); 
 
    linearLayout.findViewById(R.id.ll_config); 
 
    //final View linearLayout = getActivity().findViewById(R.id.ll_config); 
 
    linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE 
 
    linearLayout.setGravity(Gravity.CENTER); 
 

 
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
 
    LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
 

 
    //create dynamic objects inside scrollview and dynamic linear layout - horizontal 
 
    for (int i = 0; i < res2.getCount(); i++) { 
 
    LinearLayout llh = new LinearLayout(getActivity()); 
 
    llh.setOrientation(LinearLayout.HORIZONTAL); 
 
    LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
 
    llh.setLayoutParams(lp_llh); 
 

 
    linearLayout.addView(llh); 
 

 
    NumberPicker numberPicker = new NumberPicker(getActivity()); 
 
    numberPicker.setMinValue(0); 
 
    numberPicker.setMaxValue(100); 
 
    LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT); 
 
    numberPicker.setLayoutParams(lp_np); 
 
    numberPicker.setGravity(Gravity.CENTER_HORIZONTAL); 
 

 
    //showMessage("value",res2.getString(3)); 
 
    numberPicker.setValue(Integer.parseInt(res2.getString(2))); // 
 
    TextView textView = new TextView(getActivity()); 
 
    textView.setText(/*textArray[i] + " " +*/ res2.getString(1)); 
 

 

 
    llh.addView(textView); 
 
    linearLayout.addView(numberPicker); 
 

 
    } 
 
    return scrollView; 
 
}

我在这里做错了什么?

+0

你这样做完全错误检查这个示例如何添加视图运行时间https://stackoverflow.com/questions/31047502/how-to-add-view-in-a-linear-layout-dynamically – Pavan

+0

这是在使用具有动态视图的片段的上下文中。你提到的例子与它没有关系。 –

回答

0

您使用的片段u必须找到视图与片段根视图的参考也是你下面的语句做什么,我会告诉ü要经过的Android基本组件

rootview.findViewById(R.id.scroll_config); 

需要解决的问题做以下

public View viewFunds(View rootview) { 

    ScrollView scrollView = (ScrollView)rootview.findViewById(R.id.scroll_config);//considering scroll_config is scroll view in xml 
    LinearLayout linearLayout =(LinearLayout)rootview.findViewById(R.id.ll_config);//considering ll_config is LinearLayout in xml 

    linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE 
    linearLayout.setGravity(Gravity.CENTER); 

    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

    //create dynamic objects inside scrollview and dynamic linear layout - horizontal 
    for (int i = 0; i < res2.getCount(); i++) { 
    LinearLayout llh = new LinearLayout(getActivity()); 
    llh.setOrientation(LinearLayout.HORIZONTAL); 
    LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    llh.setLayoutParams(lp_llh); 

    linearLayout.addView(llh); 

    NumberPicker numberPicker = new NumberPicker(getActivity()); 
    numberPicker.setMinValue(0); 
    numberPicker.setMaxValue(100); 
    LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT); 
    numberPicker.setLayoutParams(lp_np); 
    numberPicker.setGravity(Gravity.CENTER_HORIZONTAL); 

    //showMessage("value",res2.getString(3)); 
    numberPicker.setValue(Integer.parseInt(res2.getString(2))); // 
    TextView textView = new TextView(getActivity()); 
    textView.setText(/*textArray[i] + " " +*/ res2.getString(1)); 


    llh.addView(textView); 
    linearLayout.addView(numberPicker);///chek in which layou you want to add numberPicker 

    } 
} 

和oncreateview

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false); 
    super.onCreate(savedInstanceState); 
    myDb = new DatabaseHelper(getContext()); 

    viewFunds(rootview); 
    return rootview; 
} 

也经过ndroid培训https://developer.android.com/training/index.html

+0

对不起,我不应该包含'rootview.findViewById(R.id.scroll_config)'这一行;',我之前尝试了一些东西。我已经根据您的上述建议更新了代码,但linearLayout上的动态视图仍未显示。我更新了上面的VewFunds方法,以包含我之前忘记包含的返回行。请仔细检查上面的更新代码。谢谢 –

+0

更新您的activity_settings_tab.xml问题 – Pavan

+0

你试过我的代码 – Pavan