图像加载在get视图方法处理缓慢

时间:2014-01-02 10:50:08

标签: android json listview

我正在开发一个与Web服务交互的应用程序。有一个Web服务,它提供了一个图像URL来加载应用程序中的图像。图像被加载到列表视图。所以我使用了从BaseAdapter扩展的自定义适配器。我的问题是当我在本地运行应用程序(Web服务是本地访问)它是正常的。但是,当我将Web服务URL更改为Web服务的实际位置(托管在Internet上)并运行时,显示图像需要很长时间,当我在列表视图中向上和向下滚动时,它并不顺畅。当我找到问题时,我发现图像加载部分是需要很长时间的地方。当我评论这些线时,它是正常的。请指导我解决这个问题..

这里是我的获取视图方法

public View getView(final int position, View convertView, final ViewGroup parent) {


    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row,null);


        final TextView firstname = (TextView) vi.findViewById(R.id.fname);
        final TextView lastname = (TextView) vi.findViewById(R.id.lname);
        final TextView startTime = (TextView) vi.findViewById(R.id.stime);
        final TextView endTime = (TextView) vi.findViewById(R.id.etime);
        final TextView date = (TextView) vi.findViewById(R.id.blank);
        final TextView hidID = (TextView) vi.findViewById(R.id.hidenID);
        final TextView hidAppid = (TextView) vi.findViewById(R.id.HidAppoinmentID);
         img = (ImageView) vi.findViewById(R.id.list_image);


        HashMap<String, String> song = data.get(position);

        firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        hidID.setText(song.get(MainActivity.TAG_HID));
        hidAppid.setText(song.get(MainActivity.TAG_HIDApp));

         theUrl = song.get(MainActivity.TAG_IMG);


         // BELOW LINES OF CODES UP TO ELSE PART IS THE PLACE WHICH IS TAKES VERY LONG TIME
         // WHEN I COMMENTED THESE LINES IT IS WORKED PROPERLY


        if(theUrl.equalsIgnoreCase("null")){
            Bitmap bImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.propic);
            img.setImageBitmap(bImage);

        }
        else{

        Profile pc = new Profile();

        Bitmap map =pc.downloadImage(theUrl);
        img.setImageBitmap(map);


       // imageLoader.DisplayImage(theUrl, img);
        }
        Button accept = (Button) vi.findViewById(R.id.btnAccepted);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int x = (int) getItemId(position);
                /*Intent zoom=new Intent(mContext, Profile.class);
                zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(zoom);*/

                // get the intent from the hashmap check if there is similar date and time.
                //then store them in a list or array.

                 getDate = (String) date.getText();
                 getStartTime = startTime.getText().toString();
                 getEndTime = endTime.getText().toString();
                 String appoinment = hidAppid.getText().toString();

                ShortList sh = new ShortList();

                ArrayList<HashMap<String, String>> duplicateList; 
                duplicateList=sh.getDuplicated(getDate, getStartTime, getEndTime);

                if(duplicateList.size()>1){
                    //dialogshow(getDate,getStartTime,getEndTime);
                    showFirstDialog(duplicateList);
                }
                else{
                    dialogshow(appoinment);

                }




            }
    });


        Button reject = (Button) vi.findViewById(R.id.btnReject); 
        reject.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                // get User ID  and get request ID
                //then call the method getResponceFromServerForReject()
                //final int x = (int) getItemId(position);

                String  appid = hidAppid.getText().toString();

                //Toast.makeText(mContext, c, Toast.LENGTH_LONG).show();

                dialogshowForReject(appid);
                //MainActivity ma = new MainActivity();
                //
                //ma.new JSONParse().execute();








            }
        });


        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String getPname = hidID.getText().toString();

                //Toast.makeText(parent.getContext(), "view clicked: "+getPname , Toast.LENGTH_SHORT).show();

                //get the id of the view
                //check the id of the request
                //call the web service acording to the id





                Intent zoom=new Intent(parent.getContext(), Profile.class);  
                zoom.putExtra("PatientID", getPname);
                parent.getContext().startActivity(zoom);



            }
        });

        return vi;


}

1 个答案:

答案 0 :(得分:0)

您应该使用延迟图像加载。 有很多库可以帮助您实现这一点,图像在后台加载并保存在缓存中,以便您更顺畅地滚动列表。
看看这个库: