在Android开发表中显示数据?

时间:2014-03-22 22:02:29

标签: java android

使用我的第一个Android应用程序并想在表格中显示一些数据(或类似的东西)。在环顾四周之后,我猜android不支持类似JTable的东西;有人说你要创建自定义表吗?以CSV格式显示字符串数据的最佳方法是什么?在我创建的Java应用程序中,我有一个函数,可以从字符串中创建一个2D数组并将其抛入JTable。

字符串中的数据格式:

Sun 02-16-14 09:45 PM,1,REAL TIRED,JUST WACK AT IT,2 - 4,
Sun 02-23-14 08:10 PM,1,BALLERS,REAL TIRED,4 - 11,
Sun 03-02-14 09:00 PM,1,REAL TIRED,EL TRI,1 - 7,
Sun 03-09-14 05:50 PM,1,GO GO POWER RANGERS,REAL TIRED,4 - 9,
Sun 03-16-14 06:40 PM,1,REAL TIRED,GAME OF GROANS,16 - 0,
Sun 03-23-14 09:00 PM,1,HUNGOVER HAT TRICKS,REAL TIRED,
Sun 03-30-14 07:25 PM,1,REAL TIRED,PRESTIGE WORLD WIDE,
Sun 04-06-14 04:20 PM,1,REAL TIRED,BALLERS,

由于移动设备的空间有限,不确定是否有任何好的选择。也许只能一次显示一行并能够点击它们?

1 个答案:

答案 0 :(得分:0)

您可以尝试这个示例,您将对此代码非常熟悉。即使您想要从服务器或API调用动态地在表视图中添加行,也只需创建一个for循环并根据此示例添加它。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);

    String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy",
            "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford",
            "Jimmy Carter", "Ronald Reagan", "George H. W. Bush",
            "Bill Clinton", "George W. Bush", "Barack Obama",
            "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson",
            "Richard Nixon", "Gerald Ford", "Jimmy Carter",
            "Ronald Reagan", "George H. W. Bush", "Bill Clinton",
            "George W. Bush", "Barack Obama", "Dwight D. Eisenhower",
            "John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon",
            "Gerald Ford", "Jimmy Carter", "Ronald Reagan",
            "George H. W. Bush", "Bill Clinton", "George W. Bush",
            "Barack Obama" };

    ScrollView scrollView = new ScrollView(this);

    TableLayout tableLayout = new TableLayout(this);

    for (int i = 0; i < presidents.length; i++) {

        TableRow tableRow = new TableRow(this);

        TextView tv = new TextView(this);

        tv.setPadding(10, 10, 10, 10);

        tv.setGravity(Gravity.CENTER);
        tv.setText(presidents[i]);

        tableRow.addView(tv);

        tableLayout.addView(tableRow);
        View v = new View(this);
        v.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT, 1));
        v.setBackgroundColor(Color.rgb(51, 51, 51));
        tableLayout.addView(v);
    }

    scrollView.addView(tableLayout);

        setContentView(scrollView);

    }
}

希望所以你想要这样。如果这适合您,请将其标记为已接受的答案,以便对其他人有所帮助。