Android:预加载数据库到表格布局视图

时间:2011-11-01 17:53:17

标签: android database sqlite tablelayout preload

我试图找出如何从我的资源文件夹中的预填充数据库中提取信息以显示在tablelayout视图中。如果您知道任何明确的教程,或者您知道如何做,请帮助我。

我的意思是:

数据库示例:

            Mountains      Valleys    Rivers      Oceans

Vermount yes no yes no

kentucky no yes yes no

南达科他州是否否否

(在应用程序中,如果我要显示一个特定的行,那么tablelayout应该显示为这样)

TableLayout View

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

      Mountains       yes
      Valleys          no
      Rivers           no
      Oceans           no

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

为什么要在TableLayout中添加它?

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

所以使用Multicolumn ListView 来设置列类型

中的所有值

查看Multicolumn ListView in Android

Android – Multi Column ListView

答案 2 :(得分:0)

我相信你需要将数据库文件从资产文件夹复制到其他地方才能使用它

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