在列表视图中显示位图图像

时间:2014-01-19 12:17:53

标签: android listview

我是Android新手,我希望显示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);

                }
            });

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将图像文件放在res中的drawable文件夹中,然后您可以在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);