簡體   English   中英

如何處理在Android中實現懶列表功能的動態列表?

[英]How to handle dynamic list implementing lazy list functionality in Android?

我正在開發與聊天相關的應用程序。 我的數據庫中有4500個條目,該數據庫正在顯示用戶圖像和聊天消息。 我在代碼文件下實現了更多的時間,以顯示所有建議加載到列表中的所有信息。

代碼文件:

    // Handle the messages and post messages to list view

    private void handlerMessage(final String messageType, final int dis) 
    {
         myProgressDialog = ProgressDialog.show(Shout1.this,"","Loading...",true);
         new Thread() 
         {

         public void run()
         {
             try {
                String xml;
                 Userid=getSharedPreferences("MyLoginInfo", 0).getString("user_id", "");
                if(messageType.length()==0)
                {   
                    xml="<spGetUserMessages><SearchLocation></SearchLocation><LoginUserID>"+Userid+"</LoginUserID></spGetUserMessages>";
                    messages =parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml)); 
                    Message msg = myHandler.obtainMessage();                   
                     myHandler.sendMessage(msg);
                }  
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        myProgressDialog.dismiss();
      } 
    }.start();
  }

  // Handle the messages

    private Handler myHandler = new Handler() 
    {
        public void handleMessage(Message msg) 
        {
            Log.v("Shout=====","inhandleMessage  ======="+messages.size());
            if(messages.size()==0)
            {((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
                Toast.makeText(Shout1.this, "No messages in that Location", Toast.LENGTH_SHORT).show();
            }
            else
            {
            ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
           addElements(messages);
            }
        }

    };
    //add messagess to List
    protected void addElements(final List<MessageClass> messages2) 
    {
        ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).removeAllViews();
        LinearLayout ll5=new LinearLayout(this);
        ll5.setBackgroundColor(Color.WHITE);
        ll5.setOrientation(1);

        for(int i=0;i<messages2.size();i++)
        {
                LinearLayout ll1=new LinearLayout(this);
            ll1.setPadding(0, 3, 0, 0);
            ll1.setBackgroundResource(R.drawable.tabmessage);
            ll1.setGravity(Gravity.CENTER);

            final TextView messageid = new TextView(this);
            messageid.setText(messages2.get(i).getmessageId());

            final TextView targetid = new TextView(this);
            targetid.setText(messages2.get(i).getUserid());

            final TextView targetMessage = new TextView(this);
            targetMessage.setText(messages2.get(i).getmessage());

            LinearLayout layoutmsg=new LinearLayout(this);
            layoutmsg.setClickable(true);
            layoutmsg.setOrientation(1);
            layoutmsg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            ImageView msgImage=new ImageView(this);
            final TextView msgimageInfo=new TextView(this);
            try
              {
                    String photoXml="<spGetMessagePhoto><MessageID>"+messages2.get(i).getmessageId()+"</MessageID></spGetMessagePhoto>";
                    String photoInfoFromDB1=new DataParsingComm().ILGetMessageImage(photoXml);
                    msgimageInfo.setText(photoInfoFromDB1);
                    if(photoInfoFromDB1.equalsIgnoreCase("anytype{}")|| photoInfoFromDB1==null)
                    {
                        msgImage.setVisibility(8);
                    }
                    else
                    {

                    byte[] imgArry= Base64.decode(photoInfoFromDB1);                    
                    Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);
                    msgImage.setImageBitmap(bit);
                    }
                    }catch (Exception ee) {
                        // TODO: handle exception
                        ee.printStackTrace();
                    }

        final TextView msg=new TextView(this);
        msg.setText(messages2.get(i).getmessage());

        final TextView msg1=new TextView(this); 
        if(messages2.get(i).getmessage().length()>60)
        msg1.setText(messages2.get(i).getmessage().substring(0, 60)+"...");

        else

        msg1.setText(messages2.get(i).getmessage());
        msg1.setTextColor(Color.BLACK); 
        msg1.setTypeface(Typeface.MONOSPACE);
        msg1.setTextSize(13);
        msg1.setWidth(250);
        layoutmsg.addView(msgImage);
        layoutmsg.addView(msg1);

        ImageView PersonImage=new ImageView(this);
        final TextView imageInfo=new TextView(this);
          try{
            //get the image based on user ID
            String photoXml="<spGetUserPhoto><UserID>"+messages2.get(i).getUserid()+"</UserID></spGetUserPhoto>";
            Log.v("check","photoXml.."+photoXml);   
            String photoInfoFromDB=new DataParsingComm().ILGetImage(photoXml);
            Log.v("photoInfoFromDB","photoInfoFromDB.."+photoInfoFromDB);
            imageInfo.setText(photoInfoFromDB);
            if(photoInfoFromDB.equalsIgnoreCase("anytype{}")||photoInfoFromDB.equals(null) )
            {
              PersonImage.setImageResource(R.drawable.person);
            }
            else
            {
            byte[] imgArry= Base64.decode(photoInfoFromDB);
            Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);
            Log.v("Shout","Image Lengthhhhhhhhhhhhh"+imgArry.length);
            PersonImage.setImageBitmap(bit);
            }
            }catch (Exception ee) {
                ee.printStackTrace();
            }

        final TextView personName=new TextView(this);
        if(messages2.get(i).getUname().equalsIgnoreCase("noname"))
            personName.setText("");
        else                
        personName.setText(messages2.get(i).getUname());
        personName.setTextColor(Color.rgb(0, 102, 51));
        LinearLayout layoutPersonImage=new LinearLayout(this);
        layoutPersonImage.setOrientation(1);
        layoutPersonImage.setGravity(Gravity.CENTER);
        layoutPersonImage.addView(PersonImage); 
        layoutPersonImage.addView(personName); 
        ll1.addView(layoutPersonImage);
        ll1.addView(layoutmsg);
                ll5.addView(ll1);
        }
        ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(ll5);
        }

您可以將圖像存儲在高速緩存中並訪問它。 查看此示例:

緩存中的圖像

如果我正確理解您的代碼,則您正在使用LinearLayout來顯示消息,每次收到新消息時都會重新創建整個布局。 這是一種非常低效的方法。

您應該使用ListView顯示消息列表,並使用自定義適配器加載它們。 Android處理滾動到視圖之外的項目的回收:它們被重復使用以提高內存效率。 接收新消息時無需手動重新加載每個項目,只需將新項目添加到集合中,然后在適配器上調用notifyDatasetChanged()。

請參閱Android開發人員指南中有關ListView使用適配器的文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM