简体   繁体   English

如何用 ArrayList 替换 String[]?

[英]How do I replace a String[ ] with a ArrayList?

I'm trying to populate my gridview with images using picasso.我正在尝试使用 picasso 用图像填充我的 gridview。 I'm using jsoup to collect the image links and placing them into a ArrayList.我正在使用 jsoup 来收集图像链接并将它们放入一个 ArrayList 中。 I have something wrong with my ImageAdapter cause none of my images load when I start the app.我的 ImageAdapter 有问题,导致我启动应用程序时没有加载任何图像。 My log shows the links being collected so that works.我的日志显示正在收集的链接,以便工作。 Any help will be appreciated.任何帮助将不胜感激。 I'm posting the entire code for the Activity.我正在发布活动的整个代码。

   public class MainActivity extends Activity
  {GridView grid;
String url="http://dstreet.site/";
String link,title,src;
ArrayList list= new ArrayList();



   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Scrapper();
    grid = (GridView)findViewById(R.id.grid);
    grid.setAdapter(new ImageAdapter(this, list));


    }


public class ImageAdapter extends BaseAdapter {


    ArrayList list;
    private LayoutInflater inflater;

    Context c;
     int mCount;

    ImageAdapter(Context context, ArrayList list) {
        inflater = LayoutInflater.from(context);
        c = context;

        mCount = list.size();
        this.list=list;

    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public Object getItem(int position) {
        return true; 
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View 
    convertView, ViewGroup parent) {
        final ViewHolder holder;
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.img, parent, 
       false);
            holder = new ViewHolder();
            assert view != null;

            holder.imageView = (ImageView) 
      view.findViewById(R.id.image);


            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        Picasso.get()
            .load(list.get(position))
            .placeholder(R.drawable.ic_launcher)
            .error(R.drawable.ic_launcher)
            .fit()
            .into(holder.imageView, new Callback() {

                @Override
                public void onError(Exception p1)

    {holder.imageView.setVisibility(View.INVISIBLE);
                    // TODO: Implement this method
                }


                @Override
                public void onSuccess() {

   holder.imageView.setVisibility(View.VISIBLE);
                    }


            });

        return view;
    }
}

static class ViewHolder {
    ImageView imageView;

}
public void Scrapper()
{
    Content content= new Content();
    content.execute();
}

public class Content extends 
   AsyncTask<Void,Void,Void>
{

    @Override
    protected Void doInBackground(Void[] p1)
    {
        // TODO: Implement this method

        try
        {
            Document doc = Jsoup.connect(url).get();
            // Identify Table Class "worldpopulation"
            for (Element table : 
     doc.select("div[class=poster]")) {

                Elements imgSrc = 
   table.select("img[src]");
                // Get only src from img src
                src = imgSrc.attr("src");
                list.add(src);
            }







            Log.d("image links",list.toString());
        }
        catch (IOException e)
        {e.printStackTrace();


        }
        return null;


    }

}}

如果要将 String[] 替换为 ArrayList,则需要使用 ArrayList.get(position) 而不是 IMAGE_URLS [position]。

It looks like you're not iterating over the elements after you queried the table for images:在查询表格以获取图像后,您似乎没有迭代元素:

for (Element table : doc.select("div[class=poster]")) {

    Elements imgSrc = table.select("img[src]");
    src = imgSrc.attr("src");
    list.add(src);

}

Something like this should do it:像这样的事情应该这样做:

doc.select("div[class=poster]")               // get list of div
   .stream()                                  // for each div
   .map(table -> table.select("img[src]"))    // find all images
   .flatmap(Elements::stream)                 // collapse images into 1 list
   .map(imgSrc -> imgSrc.attr("src"))         // for each image
   .forEach(list::add);                       // add to Collection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM