Skip to content Skip to sidebar Skip to footer

Retrieving Sqlite Table Data In Android App

I am retrieving my sqlite table data successfully in my Android app. However it has some minor problem which I am not able to figure out. Code Snippet: int flag=1; for (c.moveToFir

Solution 1:

I think what's happening is that the first time round (when the current row is Friday) you have flag = 1 to signal creating the columns. However the structure you have is like this:

while GetNextRow
    ifflag=1
        create cols, setflag=0else
        process row
    endif
loop

On the first time through, you get Friday, create the columns and then loop. On the second time through you get Saturday and process the row. Etc etc.

You need to take out the else. You could also put the create columns logic before the loop: that was if there is no data at all, you'd still get a screen with the columns defined and no data, rather than the blank you'd get with no data at the moment.

Cheers -

Solution 2:

Solution :

As @simon and @Andrew suggested i got rid of the if else block.And then What i did is inside the for loop i created a new Tablerow and then added views to that row.

Final Correct Code :

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

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

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


           TextView col2=newTextView(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=newTextView(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=newTextView(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=newTextView(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=newTextView(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);

            finalViewvline=newView(viewtimetable.this);
            vline.setLayoutParams(newTableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
            vline.setBackgroundColor(Color.BLUE);
            tv.addView(vline);

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



                        Stringday= c.getString(c.getColumnIndex(dba.KEY_DAY));

                        Stringslot1= c.getString(c.getColumnIndex("7:30-9:10AM"));    
                        Stringslot2= c.getString(c.getColumnIndex("9:20-11:00AM"));
                        Stringslot3= c.getString(c.getColumnIndex("11:10-12:50PM"));
                        Stringslot4= c.getString(c.getColumnIndex("1:40-3:20PM"));
                        Stringslot5= c.getString(c.getColumnIndex("3:30-5:00PM"));

                    TableRow tr1=newTableRow(viewtimetable.this);

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

                            TextView b=newTextView(viewtimetable.this);

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


                               TextView b1=newTextView(viewtimetable.this);
                               TextView b2=newTextView(viewtimetable.this);
                               TextView b3=newTextView(viewtimetable.this);
                               TextView b4=newTextView(viewtimetable.this);
                               TextView b5=newTextView(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);  
                                    }

                                 }

                                elseif(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);  
                                    }

                                 }



                                elseif(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);  
                                    }

                                 }


                                elseif(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);  
                                    }

                                 }


                                elseif(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);  
                                        }

                                     }



                                elseif(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);

Post a Comment for "Retrieving Sqlite Table Data In Android App"