0

所以我已经做了好几个小时了,现在凌晨 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 类型的数组列表中检索缩略图 url 并将它们放在一个数组中,就像我在这里做的那样

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% 确定这两种方法都以不同的方式返回相同的字符串,一种是从数组列表中的对象中获取它,另一种是我明确声明它。

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

}
}
4

1 回答 1

0

以下不起作用?

private void populateListBox()
{

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

就像在惰性列表项目中给出的示例一样。

于 2012-09-10T01:32:08.833 回答