【问题标题】:unable to display array of bitmap images in listview using simple adapter无法使用简单适配器在列表视图中显示位图图像数组
【发布时间】:2015-02-09 12:57:10
【问题描述】:

我是 android 编程新手。所以我需要帮助解决我面临的问题。 实际上我正在使用 eclipse 从 android 应用程序中的 magento 获取数据。我正在使用简单的适配器在列表视图中显示文本和图像。文本数据完美地显示在列表视图中,但我在列表视图中显示图像时遇到问题。我所做的是显示图像是:- MainActivity.java

public class MainActivity extends Activity {


    private static final String NAMESPACE = "urn:Magento";
    private static final String URL = "url of magento";
    Bitmap bitmap;
    URL imageURL = null;

    ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        TextView tv1 = (TextView) findViewById(R.id.tv);
          ListView lv = (ListView) findViewById(R.id.lv);
          ImageView   image_view = (ImageView)findViewById(R.id.imageview); 
        try {

            SoapSerializationEnvelope env = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

            env.dotNet = false;
            env.xsd = SoapSerializationEnvelope.XSD;
            env.enc = SoapSerializationEnvelope.ENC;

            SoapObject request = new SoapObject(NAMESPACE, "login");

            request.addProperty("username", "cheker");
            request.addProperty("apiKey", "123456");

            env.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,3600000);

            androidHttpTransport.call("", env);
            Object result = env.getResponse();

            Log.d("sessionId", result.toString());

            SoapSerializationEnvelope env1 = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);

            env1.dotNet = false;
            env1.xsd = SoapSerializationEnvelope.XSD;
            env1.enc = SoapSerializationEnvelope.ENC;

            SoapObject request1 = new SoapObject(NAMESPACE, "login");

            request1.addProperty("username", "cheker");
            request1.addProperty("apiKey", "123456");

            env1.setOutputSoapObject(request1);

            HttpTransportSE androidHttpTransport1 = new HttpTransportSE(URL,60000);

            androidHttpTransport1.call("", env1);
            Object result2 = env1.getResponse();

            String sessionId = result.toString();
        request = new SoapObject(NAMESPACE, "catalogProductList");
            request.addProperty("parentCategory","2" );
            request.addProperty("sessionId",sessionId );
            request.addProperty("productId",1 );

            env.setOutputSoapObject(request);
            androidHttpTransport.call("", env);


           Object   result1 = env.getResponse();
         ArrayList<String> ar = new ArrayList<String>();

            for (int i = 0; i <((SoapObject) result1).getPropertyCount(); i++) 
            {
                Object property = ((SoapObject) result1).getProperty(i);
                if (property instanceof SoapObject)
                {
                    SoapObject category_list = (SoapObject) property;
                String   mStringSchoolName = category_list.getProperty("name").toString();
               //  String      mStringSchoolGrade = category_list.getProperty("category_id").toString();
              //   String     mStringSchoolType = category_list.getProperty("parent_id").toString();


                    System.out.println("mStringSchoolName "+mStringSchoolName);
                  //  System.out.println("mStringSchoolGrade "+mStringSchoolGrade);
                  //  System.out.println("mStringSchoolType"+mStringSchoolType);
                    ar.add(mStringSchoolName+"\n\n");

                }


            }


         for (int j = 0; j <((SoapObject) result1).getPropertyCount(); j++) 
             {

              request = new SoapObject(NAMESPACE, "catalogProductAttributeMediaList");
              request.addProperty("sessionId",sessionId );
              request.addProperty("product",j+1 );
              env.setOutputSoapObject(request);
              androidHttpTransport.call("", env);
              Object resultData1 = env.getResponse();
            //  System.out.println("mStringSchoolName "+resultData1);
              Object property = ((SoapObject) resultData1).getProperty(0);
              String url=(((SoapObject)property).getProperty("url").toString());
              try {
                  imageURL = new URL(url);
                  } 

                 catch (MalformedURLException e) {
                     e.printStackTrace();
                  }

                 try {
                  HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
                  connection.setDoInput(true);
                  connection.connect();
                     InputStream inputStream = connection.getInputStream();
                       bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
                      bitmapArray.add(bitmap);
                  }
                 catch (IOException e) {

                      e.printStackTrace();
                 }

              }       


            List<HashMap<String, Bitmap>> list= new ArrayList<HashMap<String,Bitmap>>();

            for(Bitmap var: bitmapArray)
            {
             HashMap<String,Bitmap> map= new HashMap<String,Bitmap>();
                System.out.println("Item is: " + var);

                 image_view.setImageBitmap(var);

                 map.put("key",var);

                list.add(map);

            }


             String[] from = { "key" };  
            int[] to = {R.id.imageview};

            SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.activity_main,from,to);
            ListView listView = (ListView) findViewById(R.id.lv);
            listView.setAdapter(adapter); 

      } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

here in output i only get last image by using **image_view.setImageBitmap(var);** in hashmap because of not adding this in listview.but i don't want this. what i want is to display images from bitmap array that contain array of bitmaps(images)  in listview one by one at run time i.e load one by one using simple adapter.
     so please suggest me something to acheive above mentioned requirement.I will be very thankful for your suggestions.

【问题讨论】:

    标签: android magento android-listview simpleadapter


    【解决方案1】:

    您需要使用自定义适配器在列表视图中显示图像和文本... 我正在发布此链接..请检查一下...

    http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

    【讨论】:

    • 感谢 Akshey,最后我通过考虑您的建议并进行了一些更改解决了我的问题。您节省了我的时间。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2012-12-02
    • 1970-01-01
    相关资源
    最近更新 更多