使用Bitmap时图像不可见

时间:2011-07-06 11:21:12

标签: android

大家好,请指导这段代码的错误,图片在列表中不可见,虽然图片的空间和其他描述可用,谢谢

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类

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>

1 个答案:

答案 0 :(得分:2)

请在下面使用ListView中的图像延迟加载

可在此处获取资源http://open-pim.com/tmp/LazyList.zip

GitHub:https://github.com/thest1/LazyList