Bigflow Bigflow - 1 year ago 67
Android Question

Add column to table programmatically

I have some trouble to add columns to a table programmatically, maybe I look/search for the wrong keywords, but can't find a proper solution.

I got this code:

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);


What I wanted is that every String (after
try{
) is getting his own column.
So like
String result
(what is now in comment signs). but then that I can adjust the columns width.

Hope you guys know what I mean, else I can always add an image to illustrate.
Edit
Made an image fast:

enter image description here

Answer Source

You can't add Strings directly to the view, but you could use text views to do the same thing. I'll show you an example that I did once, and let you figure out how to do it for your own use.

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

Essentially, high_scores_....get(i) returns a string. Something very similar should work for your purposes.

The output will look something like this, continued down 10 rows.

name1  score1  level1
name2  score2  level2