我是安卓系统的新手,我想展示来自res的位图图像,以及ListView上的当前时间。时间显示在ListView中,但图像没有显示。谁能帮帮我吗。
我的代码如下:
public class MainActivity extends Activity {
    private int id;
    static String TIME = "time";
    static Bitmap BIMAGE;
    private Time today = new Time(Time.getCurrentTimezone());
    private Bitmap bMap;
    private ListView listView;
    private List<HashMap<String, Object>> values = new ArrayList<HashMap<String, Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        java.lang.reflect.Field[] list = R.drawable.class.getFields();
        //for(int i=0;i<list.length;i++){
        for (int i=0;i<9;i++){
        try {
            id = list[i].getInt(null);
            today.setToNow();
            //System.out.println("------------------"+id+"---------------");
            bMap = BitmapFactory.decodeResource(getResources(), id);
            bMap = Bitmap.createScaledBitmap(bMap, 50, 50, true);
            //aImage.setImageBitmap(bMap);
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("img", bMap);
            map.put("time", today.format("%k:%M"));
            values.add(map);
            new CheckinDetail().execute();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        }
    }
    class CheckinDetail extends AsyncTask<String, String, String> {
        @SuppressWarnings("deprecation")
        protected String doInBackground(String... args) {
            return null;
        }
        protected void onPostExecute(String file_url) {
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                      ListAdapter adapter = new SimpleAdapter(MainActivity.this, values, R.layout.rowlayout, new String[] { "img","time"},new int[] { R.id.AImage, R.id.time});
                      listView.setAdapter(adapter);
                      listView.setCacheColorHint(Color.TRANSPARENT);
                }
            });
        }
    }
}发布于 2014-01-19 12:44:57
您需要将yout图像文件放在res中的可绘制文件夹中,然后可以在imageView中设置XML布局中的引用,如下所示:
<ImageView
android:id="@+id/photo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/yourimage" />    更新以编程方式添加图像
ImageView image = new ImageView(context);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(pixels, pixels);
image.setLayoutParams(vp);
image.setImageResource(R.drawable.yourimage)
listView.addView(image);https://stackoverflow.com/questions/21216651
复制相似问题