代码之家  ›  专栏  ›  技术社区  ›  UMAR-MOBITSOLUTIONS

提高活动绩效的建议?

  •  0
  • UMAR-MOBITSOLUTIONS  · 技术社区  · 15 年前

    我在活动中使用以下全局变量

    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;
    
            }
    

    我已经使它们在活动中全球化,因为我需要它们在1个以上的函数中 现在我的问题是如何提高我的活动表现太慢了 我应该让它们静止还是什么?

    任何帮助都将不胜感激。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Falmarri    15 年前

    你的全局变量几乎肯定不是你表现不佳的原因。除非你访问了一百万次,否则肯定是别的什么东西。如果你告诉我们什么比你预期的执行速度慢,并发布相关代码,我们可能会提供帮助。

    你的代码太多了 getView() 方法。每次显示新视图时都会调用此方法。所以当listview被创建时,它被调用N次,其中N是所看到的列表元素的数量。当你滚动的时候,每次一个新的元素出现在屏幕上, 获取视图() 又接到电话了。即使你再向上滚动,它也会调用 获取视图() 再一次。

        2
  •  1
  •   UMAR-MOBITSOLUTIONS    15 年前

    建议您缓存图像,不要一次又一次地从internet将它们全部带来。

    所以我在使用自定义适配器和滚动时,一次又一次地从internet加载图像

    还有内存泄漏问题。

    因此,我做了什么,我遵循以下教程加载实时图像和我的问题解决了

    答:懒汉

    http://mobilebitworks.wordpress.com/2010/11/05/android-listview-and-dynamically-loading-images-from-the-web