【问题标题】:Android: Preloaded Database to table layout viewAndroid:预加载数据库到表布局视图
【发布时间】:2011-11-02 02:52:46
【问题描述】:

我一直在试图弄清楚如何从我的资产文件夹中的预填充数据库中提取信息以显示在表格布局视图中。如果您知道任何清晰的教程或者您知道如何操作,请帮助我。

我的意思的例子:

数据库示例:

            Mountains      Valleys    Rivers      Oceans

Vermount 是 否 是 否

肯塔基 不 是 是 不

南达科他州是不不不

(在应用程序中,如果我要显示特定行,那么表格布局应该是这样的)

表格布局视图

           South Dakota         <--(I know how to put the header)

      Mountains       yes
      Valleys          no
      Rivers           no
      Oceans           no

【问题讨论】:

  • 您是否对布局或数据库操作感到困惑?
  • 在某种程度上,我知道我需要使用游标从数据库中提取信息,但我不知道如何将它与 tablelayout 结合起来。

标签: android database sqlite tablelayout preload


【解决方案1】:

我必须做类似的事情,但不需要光标。这是我最终为我的项目所做的。它接受一个显示无限进度微调器的对话框。

private class AsyncPop extends AsyncTask<Dialog, Integer, ScrollView>
{
    @Override
    protected void onPostExecute( ScrollView result )
    {
        setContentView( result );
        super.onPostExecute( result );
    }

    @Override
    protected ScrollView doInBackground( Dialog... params )
    {
        Dialog d = params[0];

        Connection con = StaticDBHelper.getCon( TabActivityDetail.this.getParent() );
        Statement st = null;

        List<Timestamp> date = new ArrayList<Timestamp>();
        List<String> text = new ArrayList<String>();
        try
        {
            st = con.createStatement();
            String sql = "select activityDateTime, detailText from xfu_ActivityDetail order by activityDateTime desc";
            st.execute( sql );
            ResultSet rs = st.getResultSet();

            while( rs.next() )
            {
                date.add( rs.getTimestamp( "activityDateTime" ) );
                text.add( rs.getString( "detailText" ) );
            }
            rs.close();

        }
        catch( SQLException e )
        {
            //Can't do a whole lot about it right now, should log
        }
        finally
        {
            if( null != st )
            {
                try
                {
                    st.close();
                }
                catch( SQLException e )
                {
                    //Just ignore it, should log
                }
            }
        }
        //Formatting
        ViewGroup.LayoutParams textBoxp1 = new ViewGroup.LayoutParams( 130, ViewGroup.LayoutParams.WRAP_CONTENT );
        ViewGroup.LayoutParams textBoxp2 = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT );
        TableRow.LayoutParams rowp1 = new TableRow.LayoutParams( 130, TableRow.LayoutParams.WRAP_CONTENT );
        TableRow.LayoutParams rowp2 = new TableRow.LayoutParams( TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT );
        TableLayout.LayoutParams table1 = new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.FILL_PARENT );

        TableLayout tableView = new TableLayout( _context );
        tableView.setLayoutParams( table1 );
        if( date.size() == 0 )
        {
            TextView dateView = new TextView( _context );
            dateView.setText( "No activity details availible for given filter" );
            dateView.setTypeface( Typeface.DEFAULT, Typeface.BOLD );

            TableRow row = new TableRow( _context );
            row.setLayoutParams( new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ) );
            row.addView( dateView );
            tableView.addView( row, new TableLayout.LayoutParams( LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT ) );
        }
        else
        {
            for( int index = -1; index < date.size(); index++ )
            {
                TableRow row = new TableRow( _context );
                TextView dateView = new TextView( _context );
                TextView textView = new TextView( _context );

                if( index == -1 )
                {
                    dateView.setText( "Date / Time" );
                    textView.setText( "Detail Text" );
                    dateView.setTypeface( Typeface.DEFAULT, Typeface.BOLD );
                    textView.setTypeface( Typeface.DEFAULT, Typeface.BOLD );
                }
                else
                {
                    dateView.setText( LoaderV5._fm.format( date.get( index ) ) );
                    textView.setText( text.get( index ) );
                }
                dateView.setLayoutParams( textBoxp1 );
                textView.setLayoutParams( textBoxp2 );

                row.setLayoutParams( rowp2 );
                row.addView( dateView, rowp1 );
                row.addView( textView, rowp2 );
                tableView.addView( row, table1 );
            }
        }
        //tableView.setColumnShrinkable( 1, true );

        HorizontalScrollView otherscroller = new HorizontalScrollView( _context );
        otherscroller.addView( tableView );

        ScrollView scroller = new ScrollView( _context );
        scroller.addView( otherscroller );
        scroller.setHorizontalScrollBarEnabled( true );

        d.dismiss();
        return scroller;
    }
}

【讨论】:

    【解决方案2】:

    为什么要在 TableLayout 中添加它?

    Just fetch data from SQLite database and using custom adapter use ListView.
    

    所以使用Multicolumn ListView来设置列类型的所有值,

    Multicolumn ListView in Android

    Android – Multi Column ListView

    【讨论】:

      【解决方案3】:

      我认为您需要先将数据库文件从资产文件夹复制到其他位置,然后才能使用它

      这个问题有相关内容 Android: Accessing assets folder sqlite database file with .sqlite extension

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多