以编程方式将列添加到表

Big*_*low 3 android android-tablelayout

我以编程方式将列添加到表中有些麻烦,也许我查找/搜索错误的关键字,但找不到合适的解决方案.

我得到了这段代码:

 ScrollView sv = new ScrollView(this);

         TableLayout ll=new TableLayout(this);
         HorizontalScrollView hsv = new HorizontalScrollView(this);
         TableRow tbrow=new TableRow(this);
         for(int i=0;i<mConnector.idArray.size();i++) {
             tbrow=new TableRow(this);         

                 TextView tv1=new TextView(this);
                 //String s1 = Integer.toString(i);
                 try {
                String insecticide = mConnector.insecticideArray.get(i);
                String wegedoornuis = mConnector.wegedoornluisArray.get(i);
                String dosering = mConnector.doseringArray.get(i);
                String prijs = mConnector.prijsArray.get(i);
                String bestuivers = mConnector.bestuiversArray.get(i);
                String roofvijanden = mConnector.roofvijandenArray.get(i);                    

               // String result = insecticide +" | "+wegedoornuis+" | "+dosering+" | "+prijs+" | "+bestuivers+" | "+roofvijanden;
               // int id = Integer.parseInt(s3);
                 tv1.setId(i);

                 tv1.setText(id);
                 tbrow.addView(tv1);
                 }catch(java.lang.IndexOutOfBoundsException e){

                 }

             ll.addView(tbrow);
         }
         hsv.addView(ll);
         sv.addView(hsv);
         setContentView(sv);
Run Code Online (Sandbox Code Playgroud)

我想要的是每个String(之后try{)都有自己的专栏.所以String result(现在评论中的标志).但后来我可以调整列宽.

希望你们知道我的意思,否则我总是可以添加一张图片来说明. 编辑 快速制作图像:

在此输入图像描述

Pea*_*oto 5

您不能将字符串直接添加到视图中,但您可以使用文本视图执行相同的操作.我将向您展示我曾做过的一个例子,并让您弄清楚如何为自己使用它.

        TableRow row=new TableRow(this.getApplicationContext());
        TableLayout tlayout=new TableLayout(this.getApplicationContext());

        tlayout.setGravity(Gravity.CENTER);
        tlayout.setBackgroundResource(R.color.background);

        row=new TableRow(this);

        TextView text1=new TextView(this.getApplicationContext());
        TextView text2=new TextView(this.getApplicationContext());
        TextView text3=new TextView(this.getApplicationContext());

        text1.setText(R.string.name);
        row.addView(text1);
        text2.setText(R.string.level);
        row.addView(text2);
        text3.setText(R.string.score);
        row.addView(text3);
        tlayout.addView(row);
        for (i=0;i<10;i++)
        {
            row=new TableRow(this.getApplicationContext());
            text1=new TextView(this.getApplicationContext());
            text2=new TextView(this.getApplicationContext());
            text3=new TextView(this.getApplicationContext());


            text1.setText(high_score_name.get(i));
            row.addView(text1);
            text2.setText(""+high_score_level.get(i));
            row.addView(text2);
            text3.setText(""+high_score_score.get(i));
            row.addView(text3);
            tlayout.addView(row);
        }
Run Code Online (Sandbox Code Playgroud)

本质上,high_scores_....get(i)返回一个字符串.非常相似的东西应该适用于您的目的.

输出看起来像这样,继续向下10行.

name1  score1  level1
name2  score2  level2
Run Code Online (Sandbox Code Playgroud)