2014-01-24 23 views
-1

我在我的Android應用程序中成功檢索我的sqlite表數據。但是它有一些我無法弄清楚的小問題。檢索Android應用程序中的sqlite表數據

代碼段:

int flag=1; 
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 



        String day = c.getString(c.getColumnIndex("day")); 

        String slot1 = c.getString(c.getColumnIndex("7:30-9:10AM"));  
        String slot2 = c.getString(c.getColumnIndex("9:20-11:00AM")); 
        String slot3 = c.getString(c.getColumnIndex("11:10-12:50PM")); 
        String slot4 = c.getString(c.getColumnIndex("1:40-3:20PM")); 
        String slot5 = c.getString(c.getColumnIndex("3:30-5:00PM")); 


          TableRow tr=new TableRow(viewtimetable.this); 

          tr.setLayoutParams(new LayoutParams(
             LayoutParams.FILL_PARENT, 
             LayoutParams.WRAP_CONTENT)); 

          if(flag==1) 
          { 

           TextView col1=new TextView(viewtimetable.this); 
           col1.setText("Day"); 
           col1.setTextColor(Color.BLUE); 
           col1.setTextSize(15); 
           tr.addView(col1); 


           TextView col2=new TextView(viewtimetable.this); 
           col2.setPadding(10, 0, 0, 0); 
           col2.setTextSize(15); 
           col2.setText("7:30-9:10AM"); 
           col2.setTextColor(Color.BLUE); 
           tr.addView(col2); 

           TextView col3=new TextView(viewtimetable.this); 
           col3.setPadding(10, 0, 0, 0); 
           col3.setText("9:20-11:00AM"); 
           col3.setTextColor(Color.BLUE); 
           col3.setTextSize(15); 
           tr.addView(col3); 

           TextView col4=new TextView(viewtimetable.this); 
           col4.setPadding(10, 0, 0, 0); 
           col4.setText("11:10-12:50PM"); 
           col4.setTextColor(Color.BLUE); 
           col4.setTextSize(15); 
           tr.addView(col4); 

           TextView col5=new TextView(viewtimetable.this); 
           col5.setPadding(10, 0, 0, 0); 
           col5.setText("1:40-3:20PM"); 
           col5.setTextColor(Color.BLUE); 
           col5.setTextSize(15); 
           tr.addView(col5); 

           TextView col6=new TextView(viewtimetable.this); 
           col6.setPadding(10, 0, 0, 0); 
           col6.setText("3:30-5:00PM"); 
           col6.setTextColor(Color.BLUE); 
           col6.setTextSize(15); 
           tr.addView(col6); 

           tv.addView(tr); 

           final View vline = new View(viewtimetable.this); 

           vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
           vline.setBackgroundColor(Color.BLUE); 



          tv.addView(vline); 

          flag=0; 

          } 

          else 
          { 



         TextView b=new TextView(viewtimetable.this); 
          //String stime=json_data.getString("day"); 
          b.setText(day); 
          b.setTextColor(Color.RED); 
          b.setTextSize(15); 
          tr.addView(b); 


          TextView b1=new TextView(viewtimetable.this); 
          TextView b2=new TextView(viewtimetable.this); 
          TextView b3=new TextView(viewtimetable.this); 
          TextView b4=new TextView(viewtimetable.this); 
          TextView b5=new TextView(viewtimetable.this); 


          b1.setPadding(10, 0, 0, 0); 
          b1.setTextSize(15); 
          b1.setTextColor(Color.RED); 


          b2.setPadding(10, 0, 0, 0); 
          b2.setTextColor(Color.RED); 
          b2.setTextSize(15); 


          b3.setPadding(10, 0, 0, 0); 
          b3.setTextColor(Color.RED); 
          b3.setTextSize(15); 
          //tr.addView(b3); 

          b4.setPadding(10, 0, 0, 0); 
          b4.setTextColor(Color.RED); 
          b4.setTextSize(15); 
          //tr.addView(b4);  

          b5.setPadding(10, 0, 0, 0); 
          b5.setTextColor(Color.RED); 
          b5.setTextSize(15); 
          //tr.addView(b5); 



          if(day.equals("Monday")) 
          { 

           if(slot1 != null){ 
           b1.setText(slot1); 
           } 
           if(slot2 != null) 
           { 
            b2.setText(slot2); 
           } 
           if(slot3 != null) 
           { 
            b3.setText(slot3); 
           } 
           if(slot4 != null) 
           { 
            b4.setText(slot4); 
           } 
           if(slot5 != null) 
           { 
            b5.setText(slot5); 
           } 

          } 



          else if(day.equals("Tuesday")) 
          { 

           if(slot1 != null){ 
           b1.setText(slot1); 
           } 
           if(slot2 != null) 
           { 
            b2.setText(slot2); 
           } 
           if(slot3 != null) 
           { 
            b3.setText(slot3); 
           } 
           if(slot4 != null) 
           { 
            b4.setText(slot4); 
           } 
           if(slot5 != null) 
           { 
            b5.setText(slot5); 
           } 

          } 


          else if(day.equals("Wednesday")) 
          { 
           if(slot1 != null){ 
           b1.setText(slot1); 
           } 
           if(slot2 != null) 
           { 
            b2.setText(slot2); 
           } 
           if(slot3 != null) 
           { 
            b3.setText(slot3); 
           } 
           if(slot4 != null) 
           { 
            b4.setText(slot4); 
           } 
           if(slot5 != null) 
           { 
            b5.setText(slot5); 
           } 

          } 


          else if(day.equals("Thursday")) 
           { 
            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 

          else if(day.equals("Friday")) 
          { 

           if(slot1 != null){ 
           b1.setText(slot1); 
           } 
           if(slot2 != null) 
           { 
            b2.setText(slot2); 
           } 
           if(slot3 != null) 
           { 
            b3.setText(slot3); 
           } 
           if(slot4 != null) 
           { 
            b4.setText(slot4); 
           } 
           if(slot5 != null) 
           { 
            b5.setText(slot5); 
           } 

          }   

          else if(day.equals("Saturday")) 
            { 

             if(slot1 != null){ 
             b1.setText(slot1); 
             } 
             if(slot2 != null) 
             { 
              b2.setText(slot2); 
             } 
             if(slot3 != null) 
             { 
              b3.setText(slot3); 
             } 
             if(slot4 != null) 
             { 
              b4.setText(slot4); 
             } 
             if(slot5 != null) 
             { 
              b5.setText(slot5); 
             } 

            } 

           tr.addView(b1); 
           tr.addView(b2); 
           tr.addView(b3); 
           tr.addView(b4); 
           tr.addView(b5); 
           tv.addView(tr); 

結果:

a http://www.4shared.com/download/mhKMKta4ba/random4.png

問:

我不知道爲什麼沒有得到顯示週五的行?

P.S:由於我成功地檢索了除星期五的數據以外的所有必需數據,因此只有我猜測上述代碼段存在一些小問題。

編輯:

問題出在別的塊。由於第一個數據是星期五,並且else塊在第一次迭代中沒有執行,因此問題就出現了。

嘗試刪除else塊和它產生的異常:

指定的孩子已經有parent.you必須首先對孩子的父母打電話removeView()。

解決方案:

我做什麼是我創建了一個新的Tablerow,然後添加views到該行的循環中。

最終正確的代碼:

TableLayout tv=(TableLayout) findViewById(R.id.table); 
      tv.removeAllViewsInLayout(); 
      int flag=1; 
      TableRow tr=new TableRow(viewtimetable.this); 

      tr.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT)); 

      TextView col1=new TextView(viewtimetable.this); 
      col1.setText("Day"); 
      col1.setTextColor(Color.BLUE); 
      col1.setTextSize(15); 
      tr.addView(col1); 


      TextView col2=new TextView(viewtimetable.this); 
      col2.setPadding(10, 0, 0, 0); 
      col2.setTextSize(15); 
      col2.setText("7:30-9:10AM"); 
      col2.setTextColor(Color.BLUE); 
      tr.addView(col2); 

      TextView col3=new TextView(viewtimetable.this); 
      col3.setPadding(10, 0, 0, 0); 
      col3.setText("9:20-11:00AM"); 
      col3.setTextColor(Color.BLUE); 
      col3.setTextSize(15); 
      tr.addView(col3); 

      TextView col4=new TextView(viewtimetable.this); 
      col4.setPadding(10, 0, 0, 0); 
      col4.setText("11:10-12:50PM"); 
      col4.setTextColor(Color.BLUE); 
      col4.setTextSize(15); 
      tr.addView(col4); 

      TextView col5=new TextView(viewtimetable.this); 
      col5.setPadding(10, 0, 0, 0); 
      col5.setText("1:40-3:20PM"); 
      col5.setTextColor(Color.BLUE); 
      col5.setTextSize(15); 
      tr.addView(col5); 

      TextView col6=new TextView(viewtimetable.this); 
      col6.setPadding(10, 0, 0, 0); 
      col6.setText("3:30-5:00PM"); 
      col6.setTextColor(Color.BLUE); 
      col6.setTextSize(15); 
      tr.addView(col6); 

      tv.addView(tr); 

      final View vline = new View(viewtimetable.this); 
      vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
      vline.setBackgroundColor(Color.BLUE); 
      tv.addView(vline); 

      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 



         String day = c.getString(c.getColumnIndex(dba.KEY_DAY)); 

         String slot1 = c.getString(c.getColumnIndex("7:30-9:10AM"));  
         String slot2 = c.getString(c.getColumnIndex("9:20-11:00AM")); 
         String slot3 = c.getString(c.getColumnIndex("11:10-12:50PM")); 
         String slot4 = c.getString(c.getColumnIndex("1:40-3:20PM")); 
         String slot5 = c.getString(c.getColumnIndex("3:30-5:00PM")); 

        TableRow tr1=new TableRow(viewtimetable.this); 

           tr1.setLayoutParams(new LayoutParams(
              LayoutParams.FILL_PARENT, 
              LayoutParams.WRAP_CONTENT));  

          TextView b=new TextView(viewtimetable.this); 

           b.setText(day); 
           b.setTextColor(Color.RED); 
           b.setTextSize(15); 
           tr1.addView(b); 


           TextView b1=new TextView(viewtimetable.this); 
           TextView b2=new TextView(viewtimetable.this); 
           TextView b3=new TextView(viewtimetable.this); 
           TextView b4=new TextView(viewtimetable.this); 
           TextView b5=new TextView(viewtimetable.this); 


           b1.setPadding(10, 0, 0, 0); 
           b1.setTextSize(15); 
           b1.setTextColor(Color.RED); 


           b2.setPadding(10, 0, 0, 0); 
           b2.setTextColor(Color.RED); 
           b2.setTextSize(15); 


           b3.setPadding(10, 0, 0, 0); 
           b3.setTextColor(Color.RED); 
           b3.setTextSize(15); 


           b4.setPadding(10, 0, 0, 0); 
           b4.setTextColor(Color.RED); 
           b4.setTextSize(15); 


           b5.setPadding(10, 0, 0, 0); 
           b5.setTextColor(Color.RED); 
           b5.setTextSize(15); 


           if(day.equals("Friday")) 
           { 

            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 

           else if(day.equals("Monday")) 
           { 
            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 



           else if(day.equals("Tuesday")) 
           { 

            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 


           else if(day.equals("Wednesday")) 
           { 

            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 


           else if(day.equals("Thursday")) 
            { 

             if(slot1 != null){ 
             b1.setText(slot1); 
             } 
             if(slot2 != null) 
             { 
              b2.setText(slot2); 
             } 
             if(slot3 != null) 
             { 
              b3.setText(slot3); 
             } 
             if(slot4 != null) 
             { 
              b4.setText(slot4); 
             } 
             if(slot5 != null) 
             { 
              b5.setText(slot5); 
             } 

            } 



           else if(day.equals("Saturday")) 
             { 

              if(slot1 != null){ 
              b1.setText(slot1); 
              } 
              if(slot2 != null) 
              { 
               b2.setText(slot2); 
              } 
              if(slot3 != null) 
              { 
               b3.setText(slot3); 
              } 
              if(slot4 != null) 
              { 
               b4.setText(slot4); 
              } 
              if(slot5 != null) 
              { 
               b5.setText(slot5); 
              } 

             } 

            tr1.addView(b1); 
            tr1.addView(b2); 
            tr1.addView(b3); 
            tr1.addView(b4); 
            tr1.addView(b5); 
            tv.addView(tr1); 
+1

對於啓動,什麼是dba.KEY_DAY'的'值是多少?我想你可能在某處有錯別字 –

+1

你的循環的問題是,你的第一個數據將不會顯示,因爲它在'else-block'裏面。刪除'else'以允許顯示所有數據。我只能從圖片中猜出,我認爲你的第一個數據是星期五。編輯:我現在更加確定,因爲我看到'天'按字母順序排序。 –

+0

是的,我的第一個數據是Friday.And一天沒有按照字母順序排序,我猜!這個問題可能是因爲其他原因,但如果我刪除其他內容,那麼如果執行多次,這是嚴格意義上的部分。 –

回答

1

我認爲發生的事情是第一次輪(在當前行週五),你有標誌= 1,指示創建的列。然而你的結構是這樣的:

while GetNextRow 
    if flag = 1 
     create cols, set flag = 0 
    else 
     process row 
    endif 
loop 

第一次通過,你得到星期五,創建列,然後循環。 第二次通過你得到週六和處理行。等等。

你需要拿出其他的。你也可以在循環之前放置創建列邏輯:這就是說,如果根本沒有數據,你仍然會得到一個定義了列並且沒有數據的屏幕,而不是你在沒有數據的情況下得到的空白。時刻。

乾杯 -

+0

感謝您的回覆。我確實在循環之前創建了代碼的列,擺脫了' if和else'.現在我得到的是什麼異常是'指定的孩子已經有了一個parent.you必須首先調用孩子父母的removeView()。請檢查Edit。 ' –

+0

你能更新問題中的代碼嗎?並添加錯誤發生的行?謝謝 - –

+0

請檢查更新後的code.And我知道哪個異常(例外名稱)它正在拋出,但不知道在哪條線上生成錯誤。 –

0

解決方案:

由於@simon和@Andrew建議我擺脫了if else block的。然後我所做的是在for loop裏面,我創建了一個新的Tablerow,然後將views添加到該行。

最終正確的代碼:

TableLayout tv=(TableLayout) findViewById(R.id.table); 
      tv.removeAllViewsInLayout(); 
      int flag=1; 
      TableRow tr=new TableRow(viewtimetable.this); 

      tr.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT)); 

      TextView col1=new TextView(viewtimetable.this); 
      col1.setText("Day"); 
      col1.setTextColor(Color.BLUE); 
      col1.setTextSize(15); 
      tr.addView(col1); 


      TextView col2=new TextView(viewtimetable.this); 
      col2.setPadding(10, 0, 0, 0); 
      col2.setTextSize(15); 
      col2.setText("7:30-9:10AM"); 
      col2.setTextColor(Color.BLUE); 
      tr.addView(col2); 

      TextView col3=new TextView(viewtimetable.this); 
      col3.setPadding(10, 0, 0, 0); 
      col3.setText("9:20-11:00AM"); 
      col3.setTextColor(Color.BLUE); 
      col3.setTextSize(15); 
      tr.addView(col3); 

      TextView col4=new TextView(viewtimetable.this); 
      col4.setPadding(10, 0, 0, 0); 
      col4.setText("11:10-12:50PM"); 
      col4.setTextColor(Color.BLUE); 
      col4.setTextSize(15); 
      tr.addView(col4); 

      TextView col5=new TextView(viewtimetable.this); 
      col5.setPadding(10, 0, 0, 0); 
      col5.setText("1:40-3:20PM"); 
      col5.setTextColor(Color.BLUE); 
      col5.setTextSize(15); 
      tr.addView(col5); 

      TextView col6=new TextView(viewtimetable.this); 
      col6.setPadding(10, 0, 0, 0); 
      col6.setText("3:30-5:00PM"); 
      col6.setTextColor(Color.BLUE); 
      col6.setTextSize(15); 
      tr.addView(col6); 

      tv.addView(tr); 

      final View vline = new View(viewtimetable.this); 
      vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
      vline.setBackgroundColor(Color.BLUE); 
      tv.addView(vline); 

      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 



         String day = c.getString(c.getColumnIndex(dba.KEY_DAY)); 

         String slot1 = c.getString(c.getColumnIndex("7:30-9:10AM"));  
         String slot2 = c.getString(c.getColumnIndex("9:20-11:00AM")); 
         String slot3 = c.getString(c.getColumnIndex("11:10-12:50PM")); 
         String slot4 = c.getString(c.getColumnIndex("1:40-3:20PM")); 
         String slot5 = c.getString(c.getColumnIndex("3:30-5:00PM")); 

        TableRow tr1=new TableRow(viewtimetable.this); 

           tr1.setLayoutParams(new LayoutParams(
              LayoutParams.FILL_PARENT, 
              LayoutParams.WRAP_CONTENT));  

          TextView b=new TextView(viewtimetable.this); 

           b.setText(day); 
           b.setTextColor(Color.RED); 
           b.setTextSize(15); 
           tr1.addView(b); 


           TextView b1=new TextView(viewtimetable.this); 
           TextView b2=new TextView(viewtimetable.this); 
           TextView b3=new TextView(viewtimetable.this); 
           TextView b4=new TextView(viewtimetable.this); 
           TextView b5=new TextView(viewtimetable.this); 


           b1.setPadding(10, 0, 0, 0); 
           b1.setTextSize(15); 
           b1.setTextColor(Color.RED); 


           b2.setPadding(10, 0, 0, 0); 
           b2.setTextColor(Color.RED); 
           b2.setTextSize(15); 


           b3.setPadding(10, 0, 0, 0); 
           b3.setTextColor(Color.RED); 
           b3.setTextSize(15); 


           b4.setPadding(10, 0, 0, 0); 
           b4.setTextColor(Color.RED); 
           b4.setTextSize(15); 


           b5.setPadding(10, 0, 0, 0); 
           b5.setTextColor(Color.RED); 
           b5.setTextSize(15); 


           if(day.equals("Friday")) 
           { 

            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 

           else if(day.equals("Monday")) 
           { 
            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 



           else if(day.equals("Tuesday")) 
           { 

            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 


           else if(day.equals("Wednesday")) 
           { 

            if(slot1 != null){ 
            b1.setText(slot1); 
            } 
            if(slot2 != null) 
            { 
             b2.setText(slot2); 
            } 
            if(slot3 != null) 
            { 
             b3.setText(slot3); 
            } 
            if(slot4 != null) 
            { 
             b4.setText(slot4); 
            } 
            if(slot5 != null) 
            { 
             b5.setText(slot5); 
            } 

           } 


           else if(day.equals("Thursday")) 
            { 

             if(slot1 != null){ 
             b1.setText(slot1); 
             } 
             if(slot2 != null) 
             { 
              b2.setText(slot2); 
             } 
             if(slot3 != null) 
             { 
              b3.setText(slot3); 
             } 
             if(slot4 != null) 
             { 
              b4.setText(slot4); 
             } 
             if(slot5 != null) 
             { 
              b5.setText(slot5); 
             } 

            } 



           else if(day.equals("Saturday")) 
             { 

              if(slot1 != null){ 
              b1.setText(slot1); 
              } 
              if(slot2 != null) 
              { 
               b2.setText(slot2); 
              } 
              if(slot3 != null) 
              { 
               b3.setText(slot3); 
              } 
              if(slot4 != null) 
              { 
               b4.setText(slot4); 
              } 
              if(slot5 != null) 
              { 
               b5.setText(slot5); 
              } 

             } 

            tr1.addView(b1); 
            tr1.addView(b2); 
            tr1.addView(b3); 
            tr1.addView(b4); 
            tr1.addView(b5); 
            tv.addView(tr1); 
相關問題