简体   繁体   English

如何从数据库中获取图像并将其显示在列表视图中?

[英]How to fetch images from database and display them in listview?

I trying to fetch data from a .net web service. 我试图从.net Web服务获取数据。 I am getting json response like this . 我越来越喜欢JSON响应 But I am unable fetch the image and set it to image view. 但是我无法获取图像并将其设置为图像视图。 Coding I have done is as below. 我完成的编码如下。 Give the solution 提供解决方案

public class DisplayList extends ListActivity {

    private static final String TAG_CATEGORY = "main_name";
    private static final String TAG_SUBCAT = "cat_name";
    private static final String TAG_CODE = "code";
    private static final String TAG_NAME = "name";
    private static final String TAG_RATE = "rate";
    private static final String TAG_RATE2 = "rate2";
    private static final String TAG_RATE3 = "rate3";
    private static final String TAG_IMAGE = "img";

    ArrayList<DetailsVO> mListCustomerDetails = null;
    ArrayList<HashMap<String, String>> productlist;

    String subc;


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

        productlist = new ArrayList<HashMap<String, String>>();

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            subc = extras.getString("Subcat");
        }

        new AsyncCallSoap1().execute();
        productlist.clear();
    }

    public class AsyncCallSoap1 extends AsyncTask<String,Void,String> {
        private final ProgressDialog dialog=new ProgressDialog(DisplayList.this);

        //String tal_on=taluka.getText().toString();

        @Override
        protected String doInBackground(String... params) {
            CallSoap com=new CallSoap();
            String SOAP_ACTION="http://tempuri.org/IprotechService/SelectSuCatgeory";
            String OPERATION_NAME="SelectSuCatgeory";
            String response = com.getDetails(OPERATION_NAME,SOAP_ACTION);

            if(response!=null){
                try {

                    JSONArray array1 = new JSONArray(response);

                    for(int i=0;i<array1.length();i++)
                    {
                        JSONObject obj1 = array1.getJSONObject(i);
                        String out_category = obj1.getString(TAG_CATEGORY);
                        String out_subcat = obj1.getString(TAG_SUBCAT);
                        String out_code = obj1.getString(TAG_CODE);
                        String out_name = obj1.getString(TAG_NAME);
                        String out_rate = obj1.getString(TAG_RATE);
                        String out_rate2 = obj1.getString(TAG_RATE2);
                        String out_rate3 = obj1.getString(TAG_RATE3);
                        String out_img = obj1.getString(TAG_IMAGE);
                        // Toast.makeText(DetailsList.this, categoryId, Toast.LENGTH_LONG).show();

                        if(subc.equalsIgnoreCase(out_subcat)){
                            HashMap<String, String> details = new HashMap<String, String>();
                            details.put(TAG_NAME,out_name);
                            details.put(TAG_RATE,"Rs. "+out_rate);
                            details.put(TAG_IMAGE,out_img);

                            productlist.add(details);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //Showing progress dialog
            dialog.setMessage("Please wait...");
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            dialog.dismiss();

            ListAdapter adapter = new SimpleAdapter(
                    DisplayList.this, productlist,
                    R.layout.row_layout, new String[] {TAG_IMAGE, TAG_NAME, TAG_RATE,},
                    new int[] { R.id.imageView2,R.id.textView2,
                            R.id.textView3});

            // contactList.notifyDataSetChanged();
            setListAdapter(adapter);
        }
    }
}

You should ask the backend guy what is the root folder of the images. 您应该询问后端人员什么是图像的根文件夹。

it should look like this : " http://domain.com/images/ " 它应该看起来像这样:“ http://domain.com/images/

then you will just concat the image name: 那么您只需合并图像名称:

String image_name = "toy.png"
String image_url = "http://domain.com/images/"+image_name;

then use picasso or other way of loading image to your image view : 然后使用毕加索或其他将图像加载到图像视图的方式:

Picasso.with(context)
  .load(image_url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)

for displaying image in imageview in android first compile glide into your gradle. 用于在android中的imageview中显示图像,首先将滑行编译到gradle中。

compile 'com.github.bumptech.glide:glide:3.7.0'

then in your adapter getView method get image from url like this. 然后在您的适配器getView方法中从url这样获取图像。

ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(mContext).load(url).placeholder(R.raw.loading).error( R.drawable.nophoto ).into(imageViewTarget);
  1. If you have image as byteArray in your database means, 如果您在数据库byteArray图像作为byteArray表示,

    • Receive that byteArray , then convert it to bitmap. 接收该byteArray ,然后将其转换为位图。 using, 使用,

       Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length); 
  2. If you have image as an url means , you ca use some libraries as, 如果您将image as an url手段,则可以使用一些库,

this may helps you. 这可能对您有帮助。

暂无
暂无

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

相关问题 如何从数据库中获取数据并将其显示在ListView上? - How to fetch a data from the database and display it on ListView? 从数据库中检索图像列表并使用 ListView 显示它们? - Retrieve a list of images from the Database and display them using a ListView? 如何直接从mysql数据库中获取图像并使用php以json格式显示它们? - How to fetch images from mysql database directly and display them in json format using php.How to do this? 如何从Couchbase数据库保存和获取数据并在ListView上显示? - How to save and fetch a data from the Couchbase database and display it on ListView? 如何使用Android从数据库中的列表视图显示图像列表? - How to display list of images in listview from database using android? How to fetch images from JSON object if imagelink is given as string in android using java and display in listview using ArrayAdapters? - How to fetch images from JSON object if imagelink is given as string in android using java and display in listview using ArrayAdapters? 使用Parse.com从数据库中获取数据以显示在列表视图中 - Fetch data from database to display in a listview using Parse.com 我想从数据库中获取数据并将其显示在listview上。 - i want to fetch a data from database and display it on listview. Android:如何在ListView中显示图像? - Android:How to display images from the in a ListView? Android-如何从ListView中的URL显示图像? - Android - How to display images from URL in a ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM