简体   繁体   English

使用SimpleAdapter来自URL的图像

[英]Image from URL using SimpleAdapter

I am using this code to get data from JSON. 我正在使用此代码从JSON获取数据。

public class ShowContacts extends ListActivity{

    private static String url = "http://192.168.0.103/contacts.json";
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_NAME = "name";
    private static final String TAG_IMAGE = "image";

    JSONArray contacts = null;

    int index = 0;

    ArrayList<HashMap<String, String>> contactList;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);     
        setContentView(R.layout.showjson);

        contactList = new ArrayList<HashMap<String, String>>();
        new GetContacts().execute();

    }

    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {

            ServiceHandler sh = new ServiceHandler();           
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    for (int i = 0; i < contacts.length(); i++) {

                        JSONObject c = contacts.getJSONObject(i);

                        String name = c.getString(TAG_NAME);
                        String image = c.getString(TAG_IMAGE);

                        HashMap<String, String> contact = new HashMap<String, String>();

                        contact.put(TAG_NAME, name);
                        contact.put(TAG_IMAGE, image);

                        contactList.add(contact);

                    }

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

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);

            String[] from = { TAG_NAME, TAG_IMAGE };

            int[] to = { R.id.name, R.id.image };

            ListAdapter adapter = new SimpleAdapter(

                ShowContacts.this, contactList, R.layout.list_item, from , to );        

            setListAdapter(adapter);

        }

    }

}

I want to get the name and image from JSON. 我想从JSON获取名称和图像。 I am getting the name and it is displaying on the list, but I can't get the image. 我正在获取名称,并且该名称显示在列表中,但是我无法获取图像。 This is my JSON: 这是我的JSON:

{
    "contacts": [
        {
            "name": "John Smith",
            "image": "http://192.168.0.103/image1.png"
        },
        {                 
            "name": "John Wayne",
            "image": "http://192.168.0.103/image2.png"
        }    
    ]
}

I think it is not working because the image is online and the it tries to add it as Drawable . 我认为它不起作用,因为该图像在线,并且它试图将其添加为Drawable I am not sure how to do this. 我不确定该怎么做。 Is there any way? 有什么办法吗?

You are supposed to download the image using the LazyLoading and load that into the ImageView . 您应该使用LazyLoading下载图像并将其加载到ImageView You can not directly load the image just simply parsing and setting url into ImageView . 您不能简单地将URL解析并设置到ImageView就直接加载图像。

Use the Concept of LazyLoading or Universal ImageLoader 使用LazyLoadingUniversal ImageLoader的概念

Check the simple demostration of Loading Image from URL 检查从URL加载图像的简单演示

1)you can use Universal ImageLoader to download image from specific URL and Disply in to your List. 1)您可以使用Universal ImageLoader从特定的URL下载图像并显示在列表中。

you do not use direct imageURL to show it in list item 您不使用直接的imageURL在列表项中显示它

please check this universal image loader reference link 请检查此通用图像加载器参考链接

public class ItemImagesAdapter extends BaseAdapter { 公共类ItemImagesAdapter扩展BaseAdapter {

private Context context;
private ArrayList<String> contactList;
private ImageLoader iml;

public ItemImagesAdapter(Context ctx, ArrayList<String> image_paths) {
    context = ctx;
    contactList = image_paths;
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity).build();
    ImageLoader.getInstance().init(config);
    iml = ImageLoader.getInstance();
}

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

@Override
public Object getItem(int position) {
    return contactList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item_image, parent, false);
    }
    String imagepath = contactList.get(position).getImagePaths();
    ImageView image = (ImageView) convertView.findViewById(R.id.image1);
            TextView tv = (TextView) convertView.findViewById(R.id.text1);
            tv.setText(contactlist.get(position).getText);
    iml.displayImage(imagepath, image);
    return convertView;
}

} }

inside onPostExecute() 内部onPostExecute()

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ItemImagesAdapter adapter = new ItemImagesAdapter(getAppicationContext(), contactlist);
        listViewobj.setAdapter(adapter);
    }

call this from you mainactivity. 从您的mainactivity中调用它。 i hope you know how to set to adapter.. hope this helps you 我希望您知道如何设置适配器。希望这对您有所帮助

One and the best method for downloading images from server is Android Query . 从服务器下载图像的最佳方法之一是Android Query you can download upto 5k images without any memory error. 您最多可以下载5k图像,而不会出现任何内存错误。

Download Android query jar from Here and put in libs folder of your project. 此处 下载 Android查询jar,并将其放在项目的libs文件夹中。

AQuery aq = new AQuery(_scontext); // Android query object
ImageView _imageObject= (ImageView) itemView.findViewById(R.id.product_image); // your imageview onbject

// and using below code you can set the image in imageview. //,并使用以下代码可以在imageview中设置图像。

aq.id(_imageObject).image("image url as string");

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

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