views:

28

answers:

1

friends,

i am using following global variables in my activity

private  String Session_ID;
  private  String uid;

  // menu item starts
  private final int Trash = 0x003;
  private final int More = 0x005;
  private final int SignOut = 0x006;
  private final int SignIn = 0x007;
  //menu item ends

   private  EfficientAdapter adap;

  private  String[] Msg_id; 
  private  String[] Msg_body;
  private  String[] Sent_by;
  private  String[] Sent_on;
  private  String[] Is_my_message;
  private  String[] Photo_thumbnail;
  private  String[] Photo_full_path;


  private  String Conversation_id;
   ProgressBar progressBar;
   Button getMoreButton;


   boolean callComplete = false;
   private Handler mHandler = new Handler();
   private int PageSize = Constants.pageSizeForMessages;
   Object serviceData = null; 


   private String ConversationName;
   private Uri selectedImage;





 public  class EfficientAdapter extends BaseAdapter implements Filterable {
                    private LayoutInflater mInflater;
                    private Context context;

                    public EfficientAdapter(Context context) {
                      mInflater = LayoutInflater.from(context);
                      this.context = context;
                    }


                    public View getView(final int position, View convertView, ViewGroup parent) {
                      ViewHolder holder;
                        convertView = mInflater.inflate(R.layout.adaptor_contentmessagedetail, null);
                        holder = new ViewHolder();

                        holder.ImgPhoto = (ImageView)convertView.findViewById(R.id.ImgPhoto);
                        holder.lblMsgSentBy = (TextView) convertView.findViewById(R.id.lblSentBy);
                        holder.lblMsgBody = (TextView) convertView.findViewById(R.id.lblMessageBody);
                        holder.lblMsgSentOn = (TextView) convertView.findViewById(R.id.lblSentOn);


                        convertView.setOnClickListener(new OnClickListener() {
                              @Override
                              public void onClick(View v) {

                                  if (!((MessageDetail)v.getContext()).isConnected()) {
                                        Constants.DisplayMessage(v.getContext(),
                                                Constants.CONNECTION_ERROR_MESSAGE);
                                        return;
                                    }


                                  if(!Photo_full_path[position].equals(""))
                                  {

                                      String str= Photo_full_path[position].substring(Photo_full_path[position].length() - 3);
                                      if(str.equals("pdf"))
                                      {


                                      }else
                                      {

                                  Intent myIntent = new Intent(v.getContext(), ViewSingleImage.class);
                                    Bundle b = new Bundle();
                                    b.putString("single_image_path", Photo_full_path[position] );
                                    myIntent.putExtras(b);
                                    v.getContext().startActivity(myIntent);
                                      }

                                  }

                              }
                            });





                        convertView.setTag(holder);


                      // Bind the data efficiently with the holder.

                      if(Is_my_message[position].equals("1"))
                          holder.lblMsgSentBy.setTextColor(Color.BLACK);
                          else
                          holder.lblMsgSentBy.setTextColor(Color.rgb(255, 107, 1));



                    SimpleDateFormat fromUser = new SimpleDateFormat(Constants.SERVICE_DATE_FORMAT); 
                    java.text.DateFormat df=new SimpleDateFormat(Constants.DATE_FORMAT);                          
                    Date dt=new Date();
                    try
                    {
                        dt = fromUser.parse(Sent_on[position]);

                    } catch (java.text.ParseException e) {
                        e.printStackTrace();
                    }





                    // display photo
                    if(!Photo_thumbnail[position].equals(""))
                    {

                    // resize it
                        holder.ImgPhoto.setImageBitmap(DisplayLiveImage(Photo_thumbnail[position]));

                    }else
                    {
                        holder.ImgPhoto.setVisibility(View.GONE);

                    }
                    // display photo



                      holder.lblMsgSentBy.setText(Constants.GetSpecialCharacters(Sent_by[position]));
                      holder.lblMsgBody.setText(Constants.GetSpecialCharacters(Msg_body[position]));
                      holder.lblMsgSentOn.setText(df.format(dt));


                      return convertView;
                    }

                     class ViewHolder {

                      ImageView ImgPhoto;
                      TextView lblMsgSentBy;
                      TextView lblMsgBody;
                      TextView lblMsgSentOn;

                    }

                    @Override
                    public Filter getFilter() {
                      // TODO Auto-generated method stub
                      return null;
                    }

                    @Override
                    public long getItemId(int position) {
                      // TODO Auto-generated method stub
                      return 0;
                    }

                    @Override
                    public int getCount() {
                      // TODO Auto-generated method stub
                      return Msg_id.length;
                    }

                    @Override
                    public Object getItem(int position) {
                      // TODO Auto-generated method stub
                      return Msg_id[position];
                    }

                  } 

 public Bitmap DisplayLiveImage(String ImageSrc)
        {
            Bitmap bm;
                         try {  

                                 URL aURL = new URL(ImageSrc);  
                                 URLConnection conn = aURL.openConnection(); 

                                 conn.connect();  
                                 InputStream is = null;
                                 try
                                 {
                                     is= conn.getInputStream();  
                                 }catch(IOException e)
                                 {
                                     return null;
                                 }

                                 BufferedInputStream bis = new BufferedInputStream(is);  

                                 bm = BitmapFactory.decodeStream(bis);  
                                 bis.close();  
                                 is.close();  

                            } catch (IOException e) {  
                                return null;
                            }  

                            return  bm;

        }

i have made them global in activity because i need them all in more than 1 functions now my question is how to improve performance of my activity it is too slow should i make them static or what?

any help would be appreciated.

+1  A: 

Your global variables are almost certainly not the cause of your poor performance. Unless you're accessing them a million times, it must be something else. If you tell us what exactly is performing slower than you would expect and post the relevant code, we might be able to help.

You have a LOT of code in your getView() method. this method gets called every single time a new view gets displayed. So when the listview is created, it's called N times where N being the number of list elements that are seen. Then when you scroll, every time a new element comes onto the screen, getView() gets called again. Even if you then scroll back up, it calls getView() again.

You need to refactor your code that doesn't need to be run every time a view is created out of the view.

Falmarri
I'd add that to find out where the code is slow, you can use the traceview tool supplied in the SDK. It will help finding out where the code is spending most of its time.
Jean
it is very complex to understand.. i have used that tool nothing is understandable in it. :( which function taking how much memory etc.. any ways thanks.
UMMA
Well how long is your code? Maybe post some portions and tell us exactly what's performing slower than you'd like. If you're running network operations on the UI thread, that will certainly do it. If you're waiting for background threads that could do it. If you're looping through huge arrays that could do it too. There are an infinite number of ways to make your code slow
Falmarri
displaying live images in my custom list adapter and i think that is the reason behind updated code please take look. and thanks for your support.
UMMA