如何在UI屏幕中显示从Android中的SQLite DB检索的数据

时间:2016-01-02 16:15:57

标签: android sqlite

我已经阅读了所有可能的文章,以概述从SQLite DB中检索数据背后的逻辑。但我仍然无法找出可能的解决方案。如果有人概述了这一点会很有帮助。

例如。我只是把年龄作为用户的输入,我必须显示所有与之匹配的名称。所以在一个表中可以有n个与之匹配的数据。如何动态管理它们并在UI中显示?

是否存在要处理的预定义视图?

2 个答案:

答案 0 :(得分:0)

您可以使用TableLayout但不显示单元格边框。 或者,您可以创建自定义ListView并使用游标适配器填充它。

ListView填写CursorAdapter,请参见this 希望这会有所帮助。

答案 1 :(得分:0)

using System;
using System.Data.SqlClient;
using System.Text;

namespace DisplayDataInUI
{
    public partial class ProductListView : System.Web.UI.Page
    {
        StringBuilder table = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = "Data Source=localhost;Initial Catalog=Product; Integrated Security=True";

            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("sp_GetAllProducts", connection);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                SqlDataReader dr = cmd.ExecuteReader();
                table.Append("<table border='1'>");
                table.Append("<tr>");
                table.Append("<th>ProductID</th><th>Name</th><th>Product Type</th><th>Price</th><th>NumberofInvoices</th>");
                table.Append("</tr>");

                if(dr.HasRows)
                {
                    while(dr.Read())
                    {
                        table.Append("<tr>");
                        table.Append("<td>"+ dr[0] +"</td>");
                        table.Append("<td>"+ dr[1] +"</td>");
                        table.Append("<td>"+ dr[2] +"</td>");
                        table.Append("<td>"+ dr[3] +"</td>");
                        table.Append("<td>" + dr[4] + "</td>");
                        table.Append("</tr>");
                    }
                }
                table.Append("</table>");
                PlaceHolder1.Controls.Add(new System.Web.UI.WebControls.Literal { Text = table.ToString() });
                dr.Close();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Data retrieval Error";
                msg += ex.Message;
            }
            finally
            {
                connection.Close();
            }
        }
    }
}