Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

database - 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.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);
                              t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    if flag = 1
        create cols, set flag = 0
    else
        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 -


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...