目前的问题是点击gridview的任意item值只改变第一个item的imageview的图片,如何修改实现点击gridview的任意item值改变当前点击item的imageview的图片

极客开源灬紫易风语 发布于 2016/05/25 14:06
阅读 812
收藏 0
目前的问题是点击gridview的任意item值只改变第一个item的imageview的图片,如何修改实现点击gridview的任意item值改变当前点击item的imageview的图片。以下是代码:
package org.opensource.code.today.ui.index;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.opensource.code.today.R;
import org.opensource.code.today.model.Picture;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/5/24.
 */
public class GridViewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Picture> pictures;

    //构造函数
    public GridViewAdapter(String title, int[] images, Context context) {
        super();
        pictures = new ArrayList<Picture>();
        inflater = LayoutInflater.from(context);
        for (int i = 0; i < images.length; i++) {
            Picture picture = new Picture(title, images[i]);
            pictures.add(picture);
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (null != pictures) {
            return pictures.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        System.out.println("--" + position);
        return pictures.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        System.out.println("--1---" + position);
        return position;
    }

    @Override
        public View getView ( int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gridview_item_upload, null);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.itemText);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.itemImage);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(pictures.get(position).getTitle());
        viewHolder.image.setImageResource(pictures.get(position).getImageId());
        return convertView;
    }

    class ViewHolder {
        public TextView title;
        public ImageView image;
    }
}



package org.opensource.code.today.ui.index;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import org.opensource.code.today.R;
import org.opensource.code.today.model.Picture;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

public class UploadActivity extends Activity {

    @Bind(R.id.uploadTV)
    TextView uploadTV;

    @Bind(R.id.uGridView)
    GridView uGridView;

    public ImageView itemImage;
    public TextView itemText;

    private String title = "未选择文件";
    private int[] images = new int[] { R.mipmap.sfzzm, R.mipmap.sfzfm,
            R.mipmap.brscsfz, R.mipmap.zzzmwj, R.mipmap.xykzm,
            R.mipmap.dkytpz, R.mipmap.fcz, R.mipmap.jhz, R.mipmap.hkb,
            R.mipmap.qcxsz, R.mipmap.qtzczm};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        ButterKnife.bind(this);

        GridViewAdapter gridViewAdapter = new GridViewAdapter(title, images, this);
        uGridView.setAdapter(gridViewAdapter);
        //item点击事件
        uGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                //让图片在item中的imageview中显示的关键代码
                showDialog();
                itemImage = (ImageView)findViewById(R.id.itemImage);
                itemText = (TextView)findViewById(R.id.itemText);
            }
        });
    }

    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果
    // 创建一个以当前时间为名称的文件
    File tempFile = new File(Environment.getExternalStorageDirectory()+ "/Postcard",getPhotoFileName());

    //提示对话框方法
    private void showDialog() {
        new AlertDialog.Builder(this).setTitle("上传文件").setPositiveButton("拍照", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
// 调用系统的拍照功能
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(tempFile));
                startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
            }
        }).setNegativeButton("相册", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "image/*");
                startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
            }
        }).show();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub

        switch (requestCode) {
            case PHOTO_REQUEST_TAKEPHOTO://当选择拍照时调用
                startPhotoZoom(Uri.fromFile(tempFile), 150);
                break;

            case PHOTO_REQUEST_GALLERY://当选择从本地获取图片时
//做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
                if (data != null)
                    startPhotoZoom(data.getData(), 150);
                break;

            case PHOTO_REQUEST_CUT://返回的结果
                if (data != null)
                    setPicToView(data);
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
    private void startPhotoZoom(Uri uri, int size) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");

// aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

// outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", size);
        intent.putExtra("outputY", size);
        intent.putExtra("return-data", true);

        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    //将进行剪裁后的图片显示到UI界面上
    @SuppressWarnings("deprecation")
    private void setPicToView(Intent picdata) {

        Bundle bundle = picdata.getExtras();
        if (bundle != null) {
            Bitmap photo = bundle.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);
            itemImage.setBackgroundDrawable(drawable);
            itemText.setText("已选择文件");
        }
    }

    // 使用系统当前日期加以调整作为照片的名称
    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
        return dateFormat.format(date) + ".jpg";
    }
}





加载中
0
w
wanglinbo123
item点击事件的显示关键代码部分是什么意思
w
wanglinbo123
回复 @极客开源灬紫易风语 : 思路是点击图片,选中本地图片后 在对应位置修改数据源的信息,再次刷新适配器就可以了, 例如你在第5个图片修改了图片,那么在集合image的第五位修改图片路径
极客开源灬紫易风语
极客开源灬紫易风语
showDialog();
返回顶部
顶部