如何在ListView适配器的ImageButton中动态显示捕获的图像?

时间:2014-05-15 08:21:07

标签: android android-listview android-imagebutton

我有listview。其中包含ImageButtonButtonTextView。如果我按下imagebutton,它将进入相机。我不知道如何在imagebutton中设置图像。我的代码在这里。任何人都帮助我。

public class AddCertificateActivity extends ActionBarActivity {

    private String[] UnderGraduate={"Passport Front","Passport back","10th Mark Sheet",
            "12th Mark Sheet","IELTS or English proficiency letter by School"};

    private String[] Graduate={"Passport Front","Passport back","10th Mark Sheet",
            "12th Mark Sheet","Ist year Detail marks","2nd year Detail marks",
            "3rd year Detail marks","Final year Detail marks","Degree Copy",
            "IELTS or English proficiency letter by School","Experience Certificate( if any)"};

    private String[] PostGraduate={"Passport Front","Passport back","10th Mark Sheet",
            "12th Mark Sheet","Ist year Detail marks","2nd year Detail marks",
            "3rd year Detail marks","Final year Detail marks","Degree Copy",
            "IELTS or English proficiency letter by School","Current Resume",
            "Experience Certificate( if any)"};

    private ListView listView=null;
    private String[] data;
    private ImageButton img_Certificate;
    private TextView certificate_Name;
    private Button add;

    private static File mediaFile = null, mediaStorageDir;
    private static final String IMAGE_DIRECTORY_NAME = "Education App";
    private static final int CAMERA_REQUEST_CODE=100;
    private static final int UPLOAD_REQUEST_CODE=200;
    private static final int MEDIA_TYPE_IMAGE = 1;
    private Uri fileUri;
    public ViewHolder view;
    public CertificateAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_certificate);
        setResult(RESULT_OK);

        listView=(ListView)findViewById(R.id.listView);
        img_Certificate=(ImageButton)findViewById(R.id.img_Certificate);
        certificate_Name=(TextView)findViewById(R.id.header);
        add=(Button)findViewById(R.id.btn_Add);

        String course=getIntent().getStringExtra("Course");
        Log.i("Course", course);
        if(course.equals("UNDERGRADUATE DIPLOMA AND DEGREE"))
        {
            data=UnderGraduate;
            Log.i("If","Entered");
        }
        else if(course.equals("GRADUATE DIPLOMA"))
        {
            data=Graduate;
            Log.i("Else If","Entered");
        }
        else
        {
            data=PostGraduate;
            Log.i("Else","Entered");
        }
        Log.i("List", ""+data);
        listView.setAdapter(new CertificateAdapter(this,R.layout.list_row,data));


    }
}

public class CertificateAdapter extends ArrayAdapter<String> {

    Context context;
    String[] data;
    int layoutResourceId;
    private static final int MEDIA_TYPE_IMAGE = 1;
    private Uri fileUri;
    private static final int CAMERA_REQUEST_CODE=100;
    public Bitmap bitmap;

    public CertificateAdapter(Context context,int resources, String[] data) {

        super(context,resources,data);
        this.context=context;
        this.data=data;
        this.layoutResourceId=resources;
    }
    public class ViewHolder {
        public ImageButton btn_Image;
        public TextView text;
        public Button add;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;


        if (convertView == null) {
            LayoutInflater mInflater=((Activity)context).getLayoutInflater();
            convertView = mInflater.inflate(R.layout.list_row, null);
            holder = new ViewHolder();
            holder.btn_Image = (ImageButton)convertView.findViewById(R.id.img_Certificate);
            holder.text=(TextView)convertView.findViewById(R.id.header);
            holder.add=(Button)convertView.findViewById(R.id.btn_Add);

            convertView.setTag(holder);
        } else 
            holder = (ViewHolder) convertView.getTag();

        holder.text.setText(data[position]);

        holder.btn_Image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        ((AddCertificateActivity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

            }
        });

    return convertView;
    }

1 个答案:

答案 0 :(得分:1)

此方法放入您的activityclass

修改

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE
                && resultCode == RESULT_OK) {

            selectedImagePath = (mPhotoUri.getPath());
            if (selectedImagePath == null) {

                return;
            }
            photo = (Bitmap) BitmapFactory.decodeFile(selectedImagePath);
                    if(adapter!=null){
            adapter.btn_Image.setImageBitmap(photo);
                    }

        }

将此方法从mainactivity调用到适配器.. 像这样 :

public class CertificateAdapter extends ArrayAdapter<String> {

    AddCertificateActivity context;
    String[] data;
    int layoutResourceId;
    private static final int MEDIA_TYPE_IMAGE = 1;
    private Uri fileUri;
    private static final int CAMERA_REQUEST_CODE=100;
    public Bitmap bitmap;

    public CertificateAdapter(AddCertificateActivity context,int resources, String[] data) {

        super(context,resources,data);
        this.context=context;
        this.data=data;
        this.layoutResourceId=resources;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {



        if (convertView == null) {
            LayoutInflater mInflater=((Activity)context).getLayoutInflater();
            convertView = mInflater.inflate(R.layout.list_row, null);

            btn_Image = (ImageButton)convertView.findViewById(R.id.img_Certificate);
            text=(TextView)convertView.findViewById(R.id.header);
            add=(Button)convertView.findViewById(R.id.btn_Add);

            convertView.setTag(holder);
        }

        text.setText(data[position]);

        btn_Image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        context.startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

            }
        });

    return convertView;
    }

编辑:::

CertificateAdapter adapter=new CertificateAdapter(this,R.layout.list_row,data);
 listView.setAdapter(adapter);