0

J'ai un ScrollView avec un LinearLayout enfant. J'ajoute TextViews et NumberPickers à LinearLayout par programmation via une méthode View séparée. Cependant, les objets dynamiques ne s'affichent pas lorsque l'onglet contenant le ScrollView est cliqué.Les vues dynamiques ne s'affichent pas dans le fragment: android

Voici mon code:

@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; 
 
}

Voici la méthode séparée je l'ai mentionné qui ajoute dynamiquement des objets au 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; 
 
}

Qu'est-ce que je fais mal ici?

+0

vous faites totalement faux vérifier cet exemple comment ajouter des vues runtime https://stackoverflow.com/questions/31047502/how-to-add-view-in-a-linear-layout-dynamically – Pavan

+0

Ceci est dans le contexte de l'utilisation d'un fragment avec des vues dynamiques. L'exemple que vous avez mentionné n'est pas lié à cela. –

Répondre

0

vous utilisez fragment u doivent trouver une vue en référence de vue racine fragment également votre déclaration ci-dessous ne rien faire, je vais conseiller u de passer par des composants de base Android

rootview.findViewById(R.id.scroll_config); 

pour résoudre le problème ne suit

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 

    } 
} 

et 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; 
} 

aussi passer par un ndroid formation https://developer.android.com/training/index.html

+0

Désolé je ne devrais pas avoir inclus la ligne 'rootview.findViewById (R.id.scroll_config);', j'essayais des choses plus tôt. J'ai déjà mis à jour le code en fonction de votre suggestion ci-dessus mais les vues dynamiques sur linearLayout ne sont toujours pas affichées. J'ai mis à jour la méthode VewFunds ci-dessus pour inclure la ligne de retour que j'ai oublié d'inclure plus tôt. Vérifiez le code mis à jour ci-dessus. Merci –

+0

mise à jour votre activité_settings_tab.xml en question – Pavan

+0

avez-vous essayé mon code – Pavan