【问题标题】:Putting data from array into a view Android将数组中的数据放入视图Android
【发布时间】:2014-03-13 19:44:05
【问题描述】:

我正在尝试在 Android 应用程序中以表格的形式显示 MySQL 数据库中的数据。到目前为止,我已经使用以下代码将表中的所有数据读入数组:

 Class.forName("com.mysql.jdbc.Driver");

Connection con =     
DriverManager.getConnection("jdbc:mysql://localhost:3306/database","localhost","root");
PreparedStatement stmt = con.prepareStatement("select * from stock");
ResultSet result = stmt.executeQuery("select * from stock");

ArrayList<Integer> stockNo = new ArrayList<Integer>();
ArrayList<String> stockName = new ArrayList<String>();
ArrayList<Integer> stockQuantity = new ArrayList<Integer>();
ArrayList<String> stockUnit = new ArrayList<String>();
ArrayList<Integer> stockThreshold = new ArrayList<Integer>();

while(result.next()){   
stockNo.add(result.getInt(1));
stockName.add(result.getString(2));
stockQuantity.add(result.getInt(3));
stockUnit.add(result.getString(4));
stockThreshold.add(result.getInt(5));
}
}

我的问题是我现在如何将数组中的数据放入我的表格布局视图中?

将所有文本存储为@string 资源是一种好习惯,那么有没有办法像这样处理我的数组值?

【问题讨论】:

    标签: java android mysql arrays output


    【解决方案1】:

    你能把这五个属性封装在一个新的类中,而不是五个ArrayList吗?

    public class Stock{
            public int number;
            public String name;
            public int quantity;
            public String unit;
            public int stockThreshold;
    }
    

    并替换您的 while 循环:

    ArrayList<Stock> stockList = new ArrayList<MainActivity.Stock>();
    while(result.next()){
        Stock stock = new Stock();
        stock.number = result.getInt(1);
        stock.name = result.getString(2);
        stock.quantity = result.getInt(3);
        stock.unity = result.getString(4);
        stock.stockThreshold = result.getInt(5);
        stockList.add(stock);
        }
    

    为什么要使用 TableLayout?您应该注意内存消耗并避免创建不必要的多个视图。我推荐实现一个自定义适配器和 Gridlayout,请看这个教程(GridView 自定义适配器示例):

    http://www.mkyong.com/android/android-gridview-example/

    【讨论】:

    • 您好,感谢您的帮助!我已尝试实现您的方法,但在 new ArrayList(); 上出现错误说它“无法解析为一个类型”,你知道这是为什么吗?
    • 是的,我知道,你应该将 Stock 作为一个单独的类添加,并将 更改为 ,它应该可以解决它。
    【解决方案2】:

    可以通过以下方式完成:

    DriverManager.getConnection("jdbc:mysql://localhost:3306/database","localhost","root");
    PreparedStatement stmt = con.prepareStatement("select * from stock");
    ResultSet result = stmt.executeQuery("select * from stock");
    TableLayout tableLayout = new TableLayout(this);
    while(result.next()){
        TableRow tableRow = new TableRow(this);
        TextView stockNo = new TextView(this);
        TextView stockName = new TextView(this);
        TextView stockQuantity = new TextView(this);
        TextView stockUnit = new TextView(this);
        TextView stockThreshold = new TextView(this);
        stockNo.setText(String.valueOf(result.getInt(1)));
        stockName.setText(String.valueOf(result.getInt(2)));
        stockQuantity.setText(String.valueOf(result.getInt(3)));
        stockUnit.setText(String.valueOf(result.getInt(4)));
        stockThreshold.setText(String.valueOf(result.getInt(5)));
        tableRow.addView(stockNo);
        tableRow.addView(stockName);
        tableRow.addView(stockQuantity);
        tableRow.addView(stockUnit);
        tableRow.addView(stockThreshold);
        tableLayout.addView(tableRow);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      相关资源
      最近更新 更多