0

我想开发一个活动,以列表视图格式显示带有名称的图像列表。我有使用 BaseAdapter 的代码自定义适配器。在模拟器中运行时总是向我显示一个调试器窗口,并且模拟器上没有任何内容。

指定文件夹下有图像 (11) 图像,并授予用户权限 WRITE_EXTERNAL_STORAGE。

下面是代码。(ImageViewer.java)

package com.example.imageviewer;

import java.io.File;
import java.io.FileFilter;
import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.Inflater;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.audiofx.EnvironmentalReverb;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ImageViewer extends Activity {

    File oDirectory;
    ListView oLVImages ;

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

        try
        {
            oLVImages = (ListView) findViewById(R.id.lvImages);

            String strPath = Environment.getExternalStorageDirectory().getPath() + File.separator + Environment.DIRECTORY_PICTURES;

            oDirectory = new File(strPath);
            Log.d("ImageViewer", strPath);


            populateImages (oDirectory);
        }
        catch(Exception e){
            Log.d("ImageViewer", "1 " + e.getMessage());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_image_viewer, menu);
        return true;
    }


    public void populateImages (File oDirectory){
        try{
            File[] oFiles = oDirectory.listFiles();

            ArrayList<Image> oImages = new ArrayList<ImageViewer.Image>();

            for (int i = 0 ; i < oFiles.length;i++){
                if (oFiles[i].isFile())
                {
                    Image x = new Image();
                    x.imgPath = oFiles[i].getPath().toString();
                    x.imgName = oFiles[i].getName().toString();
                    oImages.add(x);
                }
            }

            ImageListAdapter oImageListAdapter = new ImageListAdapter(getApplicationContext(), oImages);

            oLVImages.setAdapter(oImageListAdapter);

        }
        catch (Exception e){
            Log.d("ImageViewer", e.getMessage());
        }
    }

    class Image{
        private String imgName ;
        private String imgPath;

        public Image(){
            super();
            imgName = "";
            imgPath = "";
        }

        public Image (String imgName, String imgPath){
            super();
            this.imgName = imgName;
            this.imgPath = imgPath;
        }

    }

    public class ImageListAdapter extends BaseAdapter{

        ArrayList<Image> imgImageList;
        Context oContext;

        public ImageListAdapter(Context oContext, ArrayList<Image> oImage){
            this.imgImageList = oImage;
            this.oContext = oContext;
        }
        public int getCount() {
            // TODO Auto-generated method stub
            return imgImageList.size();
        }

        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return this.imgImageList.get(arg0);
        }

        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            //super.getView(arg0, arg1, arg2);
            View oView = arg1;
            LayoutInflater inflater =  getLayoutInflater();
            oView = inflater.inflate(R.layout.imagedetail, null);
            ImageView iv = (ImageView) findViewById(R.id.ivimage);
            TextView tv = (TextView) findViewById(R.id.tvimage);
            Image oIM = imgImageList.get(arg0);
            Bitmap oBM = BitmapFactory.decodeFile(oIM.imgPath);
            iv.setImageBitmap(oBM);
            tv.setText(oIM.imgName.toString());

            return oView;
        }

    }


}

字符串.xml 文件:

<resources>

    <string name="app_name">ImageViewer</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_image_viewer">Image Viewer</string>

    <string name="ivimage"></string>
    <string name="tvheader">Photos</string>

</resources>

activity_image_viewer.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/tvheader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFFFF"
        android:padding="10dp"
        android:text="@string/tvheader"
        android:textSize="22dp"
        android:background="#FF336699"
        />
    <ListView 
        android:id="@+id/lvImages"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ListView>

</LinearLayout>

imagedetail.xml(ListView每一项的布局文件)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:stretchColumns="1"
      android:padding="10dp"
      android:id="@+id/tbllayout" >
      <TableRow
          android:id="@+id/tblrow"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
        <ImageView 
            android:id="@+id/ivimage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:padding="10dp"
            android:contentDescription="@string/ivimage"
            />

        <TextView 
            android:id="@+id/tvimage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            />
    </TableRow>
</TableLayout>
4

1 回答 1

0

在自定义适配器中使用此代码

获取视图方法

 public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            //super.getView(arg0, arg1, arg2);
            View oView = arg1;
            LayoutInflater inflater =  getLayoutInflater();
            oView = inflater.inflate(R.layout.imagedetail, null);
            ImageView iv = (ImageView)oView.findViewById(R.id.ivimage);
            TextView tv = (TextView)oView.findViewById(R.id.tvimage);
            Image oIM = imgImageList.get(arg0);
            Bitmap oBM = BitmapFactory.decodeFile(oIM.imgPath);
            iv.setImageBitmap(oBM);
            tv.setText(oIM.imgName.toString());

            return oView;
        }

布局膨胀使用小部件,

ImageView iv = (ImageView)oView.findViewById(R.id.ivimage);

TextView tv = (TextView)oView.findViewById(R.id.tvimage);

于 2012-09-12T10:13:29.330 回答