Dynamically add textViews to a linearLayout

后端 未结 6 1346
孤独总比滥情好
孤独总比滥情好 2020-11-29 00:23

I read this somewhere here and I totally lost it, but could use some assistance.

My app is pulling the column names from sqlite into an array. I want to create a tex

6条回答
  •  Happy的楠姐
    2020-11-29 01:06

    For me this is a solution.

    // Set Variables

    TextView t;
    ArrayList textViewArrayList;
    LayoutInflater layoutInflater;
    LinearLayout ll_itensobrigatorios
    

    // Tell in your onCreate

    layoutInflater = getLayoutInflater();
    createViewItem(new String[]{"Fabio", "Santos", "Programador", "Natal"});
    

    // This create view in layout

        private void createViewItem(String[] nomes) {
                textViewArrayList = new ArrayList<>();
    
                for(int i = 0; i < nomes.length; i++) {
                    View vll = layoutInflater.inflate(R.layout.nomes_tec_item, ll_itensobrigatorios, false);
                    t = (TextView) vll.findViewById(R.id.txt_tec_item);
                    textViewArrayList.add(t);
                    ll_itensobrigatorios.addView(vll);
                }
    
                for(int i = 0; i < textViewArrayList.size(); i++) {
                    textViewArrayList.get(i).setText((i + 1) + " - " + nomes[i]);
                }
    }
    

提交回复
热议问题