在android中使用url加载图像

时间:2016-01-05 10:06:07

标签: android android-layout url android-imageview

我正在尝试使用url加载图像。单个网址正常工作。但我需要在此页面中添加更多图像。我需要将此图像添加到列表视图中。请告诉我如何在此代码中添加字符串数组。

<p>Inspection and treatment will be conducted to areas prone to Mouse infestation by our Service Specialist during the routine visits. This includes the sewerage system servicing the property.</p>

<p>Apart from treating the sewerage manhole, application of residual coat will also be done at strategic areas prone to Mouse activity/harborages, and when necessary, baits will be placed and application of gel will be carried out.</p>

<p>Preparations used are safe to humans and animals and is approved by the Ministry.</p>

2 个答案:

答案 0 :(得分:0)

尝试此代码我使用 Picasso 库来填充ImageView

如果您正在使用android studio,请使用以下代码将此库添加到您的gradle文件中

compile 'com.squareup.picasso:picasso:2.5.2'

使用以下代码,

这是关于如何制作自定义ListView的完整示例,我没有包含代码,您必须从WebService获取 JSON 数据,因为我没有我想让代码变得复杂,我将编写一个单独的代码,我将展示如何阅读您感兴趣的数据。

  

XML

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ListView
        android:id="@+id/CustomListViewActivity_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
  

single_item.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/single_item_textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:layout_weight="0.5"
        android:text="New Text" />

    <ImageView
        android:id="@+id/single_item_imageView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:layout_weight="0.5" />
</LinearLayout>
  

代码

public class CustomListViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_list_view);


        ArrayList<SingleItem> singleItems = new ArrayList<>();
        singleItems.add(new SingleItem("http://movito.nervov.com/img/ace-hd.png","first Text"));
        singleItems.add(new SingleItem("http://movito.nervov.com/img/Movito_Logo_M.png","Second Text"));
        singleItems.add(new SingleItem("http://movito.nervov.com/img/ace-hd.png","third Text"));
        singleItems.add(new SingleItem("http://movito.nervov.com/img/Movito_Logo_M.png","fourth Text"));

        ListView listView = (ListView)findViewById(R.id.CustomListViewActivity_listView);
        MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.single_item,singleItems);
        listView.setAdapter(adapter);


    }

    private class MyAdapter extends ArrayAdapter {

        private ArrayList<SingleItem> singleItems;
        private LayoutInflater layoutInflater;
        private Context context;
        private View single_View;

        public MyAdapter(Context context, int resource, ArrayList<SingleItem> singleItems) {
            super(context, resource, singleItems);
            this.context = context;
            this.singleItems = singleItems;
            layoutInflater = LayoutInflater.from(this.context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder = null;

            if (row == null) {
                row = layoutInflater.inflate(R.layout.single_item, parent, false);

                holder = new ViewHolder();
                holder.textView = (TextView) row.findViewById(R.id.single_item_textView);
                holder.imageView = (ImageView) row.findViewById(R.id.single_item_imageView);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }

            final SingleItem singleItem = singleItems.get(position);
            holder.textView.setText("" + singleItem.getText());
            Picasso.with(context).load(""+singleItem.getUrl()).into(holder.imageView);

            return row;
        }

        private class ViewHolder {
            // Instance Variable (state or data)
            TextView textView;
            ImageView imageView;
        }
    }

    public class SingleItem {

        private String url;

        private String text;

        public SingleItem() {
        }

        public SingleItem(String url, String text) {
            this.url = url;
            this.text = text;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
}
  

输出

正如您将看到从提供给相应ImageView的网址中加载图片由 Picasso 处理的,请确保在{{{}中添加互联网权限1}}

AndroidManifest.xml

enter image description here

答案 1 :(得分:0)

尝试使用Glide libreries而不是picasso。它可能对您有所帮助。请查看以下可能对您有用的链接。 http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en