如何从数据库中获取图像并在listview中显示它们?

时间:2016-07-18 05:18:08

标签: android web-services

我尝试从.net网络服务中获取数据。我得到像this这样的json响应。但我无法获取图像并将其设置为图像视图。我所做的编码如下。给出解决方案

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);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您应该询问后端人员图像的根文件夹是什么。

它应该如下所示: &#34; http://domain.com/images/&#34;

然后你将只是连接图像名称:

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

然后使用毕加索或其他方式将图片加载到图片视图中:

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

答案 1 :(得分:0)

用于在android中的imageview中显示图像,首先编译滑动到你的gradle中。

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

然后在你的适配器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);

答案 2 :(得分:0)

  1. 如果数据库中的图像为byteArray,则

    • 接收byteArray,然后将其转换为位图。使用,

      Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length);
      
  2. 如果您有image as an url表示,则可以使用某些库,

  3. 这可能会对你有所帮助。