问题
Im stuck trying to figure out how to pull the information from my pre-populated database that is in my assets folder to display in a tablelayout view. If you know of any clear tutorials or if you know how to do it could you please help me out.
Example of what I mean:
Database example:
Mountains Valleys Rivers Oceans
Vermount yes no yes no
kentucky no yes yes no
South Dakota yes no no no
(In the app if I was to display a specific row then the tablelayout should appear like this)
TableLayout View
South Dakota <--(I know how to put the header)
Mountains yes
Valleys no
Rivers no
Oceans no
回答1:
I had to do something similar, but didn't need a cursor. Here is what I ended up doing for my project. It takes in a dialog that displays the infinite progress spinner.
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:
Why are you want to add it in TableLayout?
Just fetch data from SQLite database and using custom adapter use ListView.
So use Multicolumn ListView to set all values in column type,
Look at Multicolumn ListView in Android
Android – Multi Column ListView
回答3:
I believe you need to copy the Database file out from your assets folder to somewhere else before you can use it
This question has related content Android: Accessing assets folder sqlite database file with .sqlite extension
来源:https://stackoverflow.com/questions/7970609/android-preloaded-database-to-table-layout-view