【问题标题】:ListView that fetches images from URL for fragments从 URL 获取图像的 ListView 片段
【发布时间】:2014-10-06 07:12:02
【问题描述】:

我到处寻找这个和所有可能的来源,但似乎没有一个有效。我发现很多教程都展示了从 ListView 中的 URL 获取和显示图像,但它们不适用于片段。

这是我用来从 url 获取标题和链接的代码,但图像只是不来

public class FindPeopleFragment extends ListFragment {  

 TextView txtdesc;
 TextView txtdesc2;
 TextView txtdesc3;

    String flags=Integer.toString(R.drawable.logo);

    String posts;

 @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);        

   new Description().execute();   

  }

class Description extends AsyncTask<Void, Void, Void> {

    String title;
    String link;
    Bitmap bitmap = null;
    int i=0;
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    protected void onPreExecute()    {  }

    protected Void doInBackground(Void... params) {


           try {
            Document document = Jsoup.connect("http://www.marineinsight.com/shipping-news").get();

         Elements links = document.select("article a");
        //  posts = links.text();


            for(Element l : links)
            {
                Elements img = document.select("article a img[src]");
                String imgSrc = img.attr("src");
                URL newurl = new URL(imgSrc); 

                title=l.attr("title");
                if(title==""){ continue; }     
                link=l.attr("href");
                HashMap<String, String> hm = new HashMap<String,String>();
                 hm.put("txt", title);
                 hm.put("cur",link);
                 hm.put("flag",imgSrc);
                 aList.add(hm); 
                 i++;
                    }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(Void result) {

         // Keys used in Hashmap
         String[] from = { "flag","txt","cur" };

         // Ids of views in listview_layout
         int[] to = { R.id.thumb,R.id.title,R.id.date};

         // Instantiating an adapter to store each items
         // R.layout.listview_layout defines the layout of each item
         SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_item, from, to);

      setListAdapter(adapter);      
        }      
}

}

我知道hashmap&lt;string,string&gt; 将无法显示图像,但我也尝试过hashmap&lt;string,object&gt;,但效果不佳。

我也想通过片段来实现这个,因为这个列表应该是侧面导航抽屉的一部分。

【问题讨论】:

  • stackoverflow.com/questions/23985786/… 检查 Alan Deep 的回答,您只有图片网址。只有您可以在图像视图中设置后,才需要从该 url 获取图像。
  • 看看毕加索图书馆,它会让你远离痛苦的世界。非常容易使用。
  • @SathishKumar 我已经尝试过那个 helpStack 示例。但主要问题是我想在片段中执行此操作,我得到的所有示例都是为了活动。 M无法弄清楚是什么原因。谢谢
  • 解析响应后能否得到正确的结果?检查aList 已正确填充。如果你得到正确的解析数据意味着剩下的事情很简单
  • @SathishKumar 是的,除了列表视图中的图像外,一切都是正确的。不知道图像有什么问题。你能帮我修改上面的代码,以便我也可以检索图像.. 我对 android 开发完全陌生,并且在工作 2 周后能够到这里。

标签: android image listview url


【解决方案1】:

repo1.maven.org/maven2/com/squareup/picasso/picasso/2.3.4/picasso-2.3.4.jar 下载此 jar 文件并将其添加到项目的libs 文件夹中。

在您的FindPeopleFragment 中添加ImageLoaderAdapter

class ImageLoaderAdapter extends BaseAdapter {

            private List<HashMap<String, String>> dataList;

            public ImageLoaderAdapter(List<HashMap<String, String>> list) {
                this.dataList = list;
            }

            @Override
            public int getCount() {
                return dataList.size();
            }

            @Override
            public Object getItem(int arg0) {
                return null;
            }

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.list_item, null);
                }
                HashMap<String, String> map = dataList.get(position);
                ((TextView) convertView.findViewById(R.id.title)).setText(map.get("txt"));
                ((TextView) convertView.findViewById(R.id.date)).setText(map.get("cur"));
                Picasso.with(getActivity()).load(map.get("flag"))
                        .into(((ImageView) convertView.findViewById(R.id.thumb)));
                return convertView;
            }

        }

更新Description'sonPostExecute如下,

    protected void onPostExecute(Void result) {

        /*
         * // Keys used in Hashmap String[] from = { "flag","txt","cur"
         * }; // Ids of views in listview_layout int[] to = {
         * R.id.thumb,R.id.title,R.id.date}; // Instantiating an adapter
         * to store each items // R.layout.listview_layout defines the
         * layout of each item SimpleAdapter adapter = new
         * SimpleAdapter(getActivity().getBaseContext(), aList,
         * R.layout.list_item, from, to);
         */
        if (aList != null) {
            ImageLoaderAdapter adapter = new ImageLoaderAdapter(aList);
            setListAdapter(adapter);
        }else{
                Toast.makeText(getActivity(), "Data list being empty", 

Toast.LENGTH_LONG).show();
            }
}

for loop 更改为doInBackground,如下所示

                   for(Element l : links)
                   {
                        Elements img = l.select("img[src]");//This line changed
                        String imgSrc = img.attr("src");//This line changed
                      // URL newurl = new URL(imgSrc); 

                       title=l.attr("title");
                       if(title==""){ continue; }     
                       link=l.attr("href");
                       HashMap<String, String> hm = new HashMap<String,String>();
                        hm.put("txt", title);
                        hm.put("cur",link);
                        hm.put("flag",imgSrc);
                        aList.add(hm); 
                        i++;
                   }

了解更多关于图书馆check it

【讨论】:

  • 哇 @SathishKumar 你刚刚搞定了.. 终于我有了图片.. 请再帮我一个.. 我得到的图片对于所有项目都是一样的(第一篇文章的缩略图见于全部)。我如何给每篇文章各自的缩略图..是的,我真是太感谢你了:)
  • 您在doInBackground 中的代码Elements img = document.select("article a img[src]"); String imgSrc = img.attr("src"); 处有错误,请检查。这里你需要从Element对象l而不是Elements对象img获取imgsrc
  • 感谢您指出错误...我已经意识到它不应该那样,但我如何获取两个'元素链接 = document.select("article a");'和'元素 img = document.select("article a img[src]");'在同一个“for”循环中?我尝试为“链接”和“img”运行单独的循环,但随后应用程序崩溃了。我现在做什么?
  • 请发布您的article标签数据
  • 文章标签看起来像这样&lt;article class="post-60739 category-shipping-news" itemscope="itemscope" itemtype="http://schema.org/BlogPosting"&gt; &lt;a href="http://xyz" title="abc" class="alignleft"&gt; &lt;img src="http://www.marineinsight.com/wp-content/uploads/2014/10/sea-trails-75x75.png" class="entry-image attachment-post" itemprop="image" /&gt; &lt;/a&gt; &lt;header class="entry-header"&gt;&lt;h2 class="entry-title"&gt; &lt;a href="http://xyz"&gt;Video: Ghost &amp;#8211; Stealth Ship of Future&lt;/a&gt;&lt;/h2&gt;&lt;/header&gt; &lt;/article&gt;
【解决方案2】:

如果在 String 中获取图像,则首先将该 imageSting 转换为位图。试试下面的代码

public static Bitmap StringToBitMap(String encodedString) {
        try {
            byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
                    encodeByte.length);
            return bitmap;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }

现在你可以使用HashMap&lt;String,Object&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多