Tejas
Tejas

Reputation: 21

Android dynamically drawing more than 1000 text view is causing full application to not responding

This is my function which will fetch some 1300 record from SQLite database and draw a series of text view inside the table layout. But when this function starts executing full application is not responding for some time. Table layout is again inside a Scroll View.

Basically I want it to load in back ground.so that remaining application is still responsive

   public void LoadAlarmNotifications() {

     int SerNo = 0 ;
     TextView SerNoTxtVw;
     TextView AlarmTxtVw;
     TextView TimeOccuredTxtVw;  
     TextView UploadTxtTxtVw;  
     Notificationtablelayout.setGravity(Gravity.CENTER);

     pastEvent_tbleList  = oDatabaseHandler.FetchPastEventDetails(sDiagnosisID);

     if(pastEvent_tbleList != null)
       {
            for(Pastevents_tble PEvents_tble: pastEvent_tbleList)
            {
                     final TableRow row = new TableRow(this.getApplicationContext());
                     nAlarmType          = PEvents_tble.GetAlarmID();
                     nRowID              = PEvents_tble.GetEventNo(); 
                     SerNo               = PEvents_tble.GetEventNo();
                     SetAlarmType(nAlarmType);  // Set Alarm
                     tAlarmTime          = PEvents_tble.GetStrtTime(); 
                     sUploadStatus       = PEvents_tble.GetUploadStatus();
                     row.setId(nRowID);

                     SerNoTxtVw      =new TextView(this.getApplicationContext());
                     AlarmTxtVw      =new TextView(this.getApplicationContext());
                     TimeOccuredTxtVw=new TextView(this.getApplicationContext());
                     UploadTxtTxtVw  =new TextView(this.getApplicationContext()); 

                     SerNoTxtVw.setText(Integer.toString(SerNo));
                     SerNoTxtVw.setWidth(80);
                     SerNoTxtVw.setHeight(40);
                     SerNoTxtVw.setGravity(Gravity.CENTER);
                     SerNoTxtVw.setPadding(2, 2,2,2);
                     SerNoTxtVw.setTextColor(Color.parseColor("#FFAADDFF"));
                     row.addView(SerNoTxtVw);

                     AlarmTxtVw.setText(sAlarmName);
                     AlarmTxtVw.setWidth(100);
                     AlarmTxtVw.setHeight(40);
                     AlarmTxtVw.setGravity(Gravity.LEFT);
                     AlarmTxtVw.setPadding(2,2,2,2);
                    // AlarmTxtVw.setBackgroundColor(Color.LTGRAY);
                     row.addView(AlarmTxtVw);

                     TimeOccuredTxtVw.setText(tAlarmTime);
                     TimeOccuredTxtVw.setWidth(140);
                     TimeOccuredTxtVw.setHeight(40);
                     TimeOccuredTxtVw.setGravity(Gravity.CENTER);
                     TimeOccuredTxtVw.setPadding(2,2,2,2);
                    // TimeOccuredTxtVw.setBackgroundColor(Color.LTGRAY);
                     row.addView(TimeOccuredTxtVw);

                     UploadTxtTxtVw.setText(sUploadStatus);
                     if(sUploadStatus.equals("Y")){
                         UploadTxtTxtVw.setText("✔");
                         UploadTxtTxtVw.setTextColor(Color.parseColor("#FF00FF00")); 
                     }
                     else if(sUploadStatus.equals("N")){
                         UploadTxtTxtVw.setText("X");
                         UploadTxtTxtVw.setTextColor(Color.parseColor("#FFFF0000")); 
                      }else
                      {
                         UploadTxtTxtVw.setText("✔");
                         UploadTxtTxtVw.setTextColor(Color.parseColor("#FFAADDFF"));
                      }

                     UploadTxtTxtVw.setWidth(100);
                     UploadTxtTxtVw.setHeight(40);
                     UploadTxtTxtVw.setGravity(Gravity.CENTER);
                     UploadTxtTxtVw.setPadding(2,2,2,2);
                    // UploadTxtTxtVw.setBackgroundColor(Color.LTGRAY);
                     row.addView(UploadTxtTxtVw);

                     Notificationtablelayout.addView(row); 


                     nProgressCounter++;

                     row.setOnClickListener(new OnClickListener() {

                @Override
                    public void onClick(final View SelectedRowVw) {  
                          if(!bSwitchPopOpen)
                          {

                          }

                    });
            }  

            LoadingTxtView.setVisibility(View.GONE);
            bEpisodeLoad = true;
       } else
       {
            bEpisodeLoad = true;
            LoadingTxtView.setText("No Snapshot available...");

       } 
}

pastEvent_tbleList size is 1335. Any idea how to optimize this. Instead of text view is there any other light control?

Upvotes: 0

Views: 183

Answers (2)

Ragaisis
Ragaisis

Reputation: 2750

You should use listview, because it loads visible area so application will not stop doing calculations and you can scroll up and down and see your texviews

Upvotes: 1

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95479

You should use the Loader pattern to do this so that you only load content that the user will actually see, and you should also reuse the text views on the screen. This is what ListView / CursorLoader were created to solve, so you really ought to reuse these components if at all possible.

Upvotes: 3

Related Questions