程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-使用 Bitmap 時,圖片不能顯示

android-使用 Bitmap 時,圖片不能顯示

編輯:編程綜合問答
使用 Bitmap 時,圖片不能顯示

請大家幫我看看下面的代碼,為什麼圖像不能在list中顯示,然而空間和圖像的其他描述卻能看得見。

public class Test extends ListActivity  {
      Prefs myprefs = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        this.myprefs = new Prefs(getApplicationContext());
        // install handler for processing gui update messages
        ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); 
       JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=" + 
        Test.this.myprefs.getPersonalno());
        try{

            JSONArray  earthquakes = json.getJSONArray("services");

            for(int i=0;i<earthquakes.length();i++){                        
                HashMap<String, Object> map = new HashMap<String, Object>();
                 JSONObject e = earthquakes.getJSONObject(i);

                map.put("id", e.getString("taskid"));
                map.put("pic", "Service name : " + e.getString("employeepic"));
                map.put("serviceinfo", "" +  e.getString("employeename")+ " : "+ e.getString("starttime")
                        +" To " +  e.getString("endtime"));

                  BmpFromURL  myBmpFromURL = new BmpFromURL("http://midsweden.gofreeserve.com/proj/admin/pictures/file87619.jpg");
                  Bitmap myBitmap = myBmpFromURL.getMyBitmap();
                    map.put("img",myBitmap);
                mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,new String[] {"img", "servicename", "serviceinfo" }, 
                        new int[] {  R.id.image ,R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
      @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:     
                Intent intent = new Intent(this, ShowSettings.class);
                  startActivity(intent);
            break;
            case R.id.web:     
                Intent intent4 = new Intent(this, MenuActivity.class);
                  startActivity(intent4);
            break;
            case R.id.services:     
                Intent intent2 = new Intent(this, Test.class);
                startActivity(intent2);
              break;
            case R.id.Quit: 
               Intent intent3 = new Intent(this, ServicesDemo.class);
                startActivity(intent3);
                   break;
            default:    
            break;
        }
        return true;
    }
}

Bitmap class

    public class BmpFromURL {
    private Bitmap myBitmap;
    public BmpFromURL(String imageURL){
    URL myImageURL = null;
    try {
    myImageURL = new URL(imageURL);

    } catch (MalformedURLException error) {


    error.printStackTrace();

    }
    try {
    HttpURLConnection connection = (HttpURLConnection)myImageURL .openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    myBitmap = BitmapFactory.decodeStream(input);

    } catch (IOException e) {
   e.printStackTrace();
    }
    }
   public Bitmap getMyBitmap() {
    return myBitmap;
    }
}

xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#55000000"
    android:background="#55000000"
    >
      <ImageView
      android:id="@+id/image"
      android:layout_width="50dip"
      android:layout_height="50dip"
      />
<TextView  

    android:id="@+id/item_title"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:padding="2dp"
    android:textSize="20dp" />
    <TextView  
    android:id="@+id/item_subtitle"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp"
    android:textSize="18dp" />
</LinearLayout>

最佳回答:


請使用下面 ListView 的圖片延遲加載
請看這裡: http://open-pim.com/tmp/LazyList.zip
GitHub: https://github.com/thest1/LazyList

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved