android懒惰列表图像加载数组

时间:2012-09-10 01:14:32

标签: android image listview lazy-loading

所以我已经在这里工作了几个小时而且它早上4:07现在我必须睡觉,所以我希望有人可以帮助我。

我有一个ImageResults对象的ArrayList,其类定义为:

public class ImageResults {

 String _title, _country, _thumbnailURL, _imageURL;

 public ImageResults(String title, String country, String thumbnailURL, String imageURL)
 {
     _title = title;
     _country = country;
     _thumbnailURL = thumbnailURL;
     _imageURL = imageURL;
 }

 public String getTitle()
 {
     return _title;
 }


 public String getCountry()
 {
     return _country;
 }

 public String getThumbnailURL()
 {
     return _thumbnailURL;
 }

 public String getImageURL()
 {
     return _imageURL;
 }
}

现在为了使用https://github.com/thest1/LazyList,我必须从我的类型为imageresults的arraylist中检索缩略图网址,并将它们放在像我这样做的数组中

private void populateListBox()
{
    String[] imgLst = new String[imagesList.size()];

    for(int i = 0; i < imagesList.size();i++)
    {
        imgLst[i] = (imagesList.get(i)._thumbnailURL);
    //  Toast t = Toast.makeText(this,imgLst[0] , Toast.LENGTH_SHORT);
    //  t.show();
    }

    adapter=new LazyAdapter(this, imgLst);
    imageListView.setAdapter(adapter);
}

现在问题就是上面的方式不起作用,但如果我单独使用链接,如下所示,它是原始项目中链接组织的默认方式

private void populateListBox()
{
     String[] imgLst={
            "http://www.istartedsomething.com/bingimages/resize.php?i=Velodrome_EN-AU1182456710.jpg&w=300"};

    adapter=new LazyAdapter(this, imgLst);
    imageListView.setAdapter(adapter);
}

这就是原始项目中链接的组织方式,并且我100%确定两个方法只是以不同的方式返回相同的字符串,一个是从arraylist中的对象获取,另一个是明确声明它

 private String[] mStrings={
        "http://www.istartedsomething.com/bingimages/resize.php?i=Velodrome_EN-AU1182456710.jpg&w=300",
        "http://www.istartedsomething.com/bingimages/resize.php?i=Velodrome_EN-CA1182456710.jpg&w=300",
        "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
        "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
        "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
        "http://a3.twimg.com/profile_images/511790713/AG.png",
        "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
        "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
        "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
        "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg"};

主要活动类

public class BngPaperActivity extends Activity {

ListView imageListView;
Spinner countrySpinner;
String selectedMonth;
String selectedYear;

LazyAdapter adapter;

ProgressDialog progress;

Dialog date;

getResult getRes;

String ResultsString;

ArrayList<String> monthList = new ArrayList<String>();
ArrayList<ImageResults> imagesList = new ArrayList<ImageResults>();
String dateText;

TextView selectedDateView;
static final int MONTHYEARDATESELECTOR_ID = 3;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    progress =  new ProgressDialog(BngPaperActivity.this);

    monthList.add("January"); monthList.add("February"); monthList.add("March"); monthList.add("April");
    monthList.add("May"); monthList.add("June"); monthList.add("July"); monthList.add("August");
    monthList.add("September"); monthList.add("October"); monthList.add("November"); monthList.add("December");

    imageListView = (ListView) this.findViewById(R.id.imagesListView);
    countrySpinner = (Spinner) this.findViewById(R.id.countrySpinner);
    selectedDateView = (TextView) this.findViewById(R.id.selectedDateView);



    Button monthYearButton = (Button) this.findViewById(R.id.monthyearBTN);
    // set up a listener for when the button is pressed
    monthYearButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            // call the internal showDialog method using the predefined ID

            showDialog(MONTHYEARDATESELECTOR_ID);
        }
    });

}



private DateSlider.OnDateSetListener mMonthYearSetListener =
    new DateSlider.OnDateSetListener() {
        public void onDateSet(DateSlider view, Calendar selectedDate) {
            // update the dateText view with the corresponding date
            dateText = (String.format("%tB %tY", selectedDate, selectedDate));
            selectedDateView.setText(dateText);
            try {

                 selectedMonth = monthList.indexOf(String.format("%tB", selectedDate)) + 1 +"";
                 selectedYear = String.format("%tY", selectedDate);

                 progress.setMessage("Fetching Images... \nPress Back Button To Cancel");
                 progress.setCancelable(true);


                getRes = new getResult(progress,view);
                getRes.execute();

                try {
                    ResultsString = getRes.get();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                parseResults(ResultsString);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
};

private void parseResults(String result)
{
  Scanner scan = new Scanner(result);

  String current = scan.nextLine();
  String title = "";
  String country = "";
  String thumbURL = "";
  String imageURL = "";


  while(!current.equals("End of file"))
  {
      if(current.equals("Begin Thumb"))
      {
          current = scan.nextLine();
          title = current.substring(current.indexOf(":")+1);

          current = scan.nextLine();
          country = current.substring(current.indexOf(":")+1);

          current = scan.nextLine();
          thumbURL = current.substring(current.indexOf(":")+1);

          imageURL = thumbURL.replace("300", "900");

          current = scan.nextLine();
      }

      if(current.equals("End Thumb"))
      {
          imagesList.add(new ImageResults(title,country,thumbURL,imageURL));
      }

      current = scan.nextLine();
  }

  populateListBox();

}

private void populateListBox()
{

    //this is not working, i would like this one to work

        String[] imgLst = new String[imagesList.size()];

        for(int i = 0; i < imagesList.size();i++)
        {
            imgLst[i] = (imagesList.get(i)._thumbnailURL);
        //  Toast t = Toast.makeText(this,imgLst[0] , Toast.LENGTH_SHORT);
        //  t.show();
        }
    //-----------------------------------


        /* This is working
            String[] imgLst={
                    "http://www.istartedsomething.com/bingimages/resize.php?i=Velodrome_EN-AU1182456710.jpg&w=300"};
         */
    adapter=new LazyAdapter(this, imgLst);
    imageListView.setAdapter(adapter);
}


@Override
protected Dialog onCreateDialog(int id) {
    // this method is called after invoking 'showDialog' for the first time
    // here we initiate the corresponding DateSlideSelector and return the dialog to its caller

    final Calendar c = Calendar.getInstance();

    final Calendar minDate = Calendar.getInstance();
        minDate.set(Calendar.YEAR, 2009);
        minDate.set(Calendar.MONTH, Calendar.JUNE);
    final Calendar maxDate = Calendar.getInstance();
        maxDate.add(Calendar.DATE, 0);


    switch (id) {

    case MONTHYEARDATESELECTOR_ID:
        return new MonthYearDateSlider(this,mMonthYearSetListener,c,minDate,maxDate);

    }
    return null;
}

private class getResult extends AsyncTask<String, String, String> {


    private final HttpClient Client = new DefaultHttpClient();
    private String Content;
    private String Error = null;
    private ProgressDialog progress;
    DateSlider view;

    public getResult(ProgressDialog progress, DateSlider view) 
    {
        this.progress = progress;
        this.view = view;
    }


    protected void onPreExecute() {

        this.view.dismiss();
        this.progress.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpResponse response; 
        String responseString = null; 
        try { 
            response = httpclient.execute(new HttpGet("http://devleb.com/BngPaper/BngPaperWebService.php?thumbnail=Yes&year="+selectedYear+"&month="+ selectedMonth)); 
            StatusLine statusLine = response.getStatusLine(); 
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
                ByteArrayOutputStream out = new ByteArrayOutputStream(); 
                response.getEntity().writeTo(out); 
                out.close(); 
                responseString = out.toString(); 
            } else{ 
                //Closes the connection. 
                response.getEntity().getContent().close(); 
                throw new IOException(statusLine.getReasonPhrase()); 
            } 
        } catch (ClientProtocolException e) { 
            //TODO Handle problems.. 
        } catch (IOException e) { 
            //TODO Handle problems.. 
        } 
        //Dialog.dismiss();

        progress.dismiss();
        return responseString; 
    }

    protected void onPostExecute(Void unused) {

        this.progress.dismiss();

        if (Error != null) {
            Toast.makeText(BngPaperActivity.this, Error, Toast.LENGTH_LONG).show();
        } 
    }

}
}

1 个答案:

答案 0 :(得分:0)

以下不起作用?

private void populateListBox()
{

    adapter=new LazyAdapter(this, mStrings);
    imageListView.setAdapter(adapter);
}

与lazylist项目中给出的示例类似。