从String数组创建表

时间:2016-03-06 10:35:28

标签: java android arrays

我有一个String数组:

String[] arr = {
            "04:20",
            "05:25", "05:41", "05:57",
            "06:13", "06:29", "06:37", "06:45", "06:53",
            "07:01", "07:09", "07:17", "07:25", "07:33", "07:41", "07:49", "07:57",
            "08:05", "08:13", "08:21", "08:29", "08:37", "08:45", "08:53"
    };

我想从这个String数组this table

创建

实施该方法的更好方法是什么?我在考虑TableLayoutHTML我不知道哪个更好。

问题:如何从字符串数组动态创建表?

我的代码:

 String tempHours = "00:";
    ArrayList<Items> items_data = new ArrayList<>();
    for(String s: arr){
        String[] split = s.split(":");
        String hours = split[0] + ":";
        if(tempHours.equals(hours)) {
            items_data.add(new Items(split[0], split[1]));
        } else {
            tempHours = hours;
            items_data.add(new Items("\n" + split[0],split[1]));
        }
    }

    TableLayout tl = new TableLayout(this);

    for (Items items : items_data) {
        TableRow tr = new TableRow(this);

        TextView hours = new TextView(this);
        hours.setText(items.getHours());
        tr.addView(hours);

        TextView minutes = new TextView(this);
        minutes.setText(items.getMinutes());
        tr.addView(minutes);


        tl.addView(tr);
    }

项目:

public class Items {

String hours;
String minutes;

public Items(String hours, String minutes) {
    this.minutes = minutes;
    this.hours = hours;
}

public String getMinutes() { return minutes; }
public String getHours() { return hours; }

}

0 个答案:

没有答案