2011-11-01 40 views
0

我試圖弄清楚如何從我的資產文件夾中的預填充數據庫中提取信息以顯示在tablelayout視圖中。如果你知道任何明確的教程,或者如果你知道如何做到這一點,你可以幫助我。我的意思Android:預加載數據庫到表佈局視圖

例子:

數據庫例如:

  Mountains  Valleys Rivers  Oceans 

Vermount是否是否

肯塔基否是是

南達科他州是否否否

(在應用程序中,如果我要顯示一個特定IC行則tablelayout應該出現這樣的)

TableLayout查看

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

     Mountains  yes 
     Valleys   no 
     Rivers   no 
     Oceans   no 
+1

你感到困惑複製這樣做佈局,還是數據庫操作? – Codeman

+0

什麼不工作? –

+0

在兩種方式中,我知道我需要使用遊標從數據庫中提取信息,但我不知道如何將它與tablelayout結合起來。 – Nate

回答

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; 
    } 
}