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