GridView - 在新活动中打开图像

时间:2016-12-08 20:22:08

标签: android gridview textview imageview

目前我的项目有一个GridView,每个都有图像和描述。单击图像时,它应该在新活动中打开,其中包含完整的图像和描述。

它目前在模拟器上工作正常,但onclick在真实设备上什么都不做。

如果我在MainActivity中将intent.putExtra("image", item.getImage())更改为intent.putExtra("image", bitmap),则会打开新的辅助DetailsActivity,但仅显示文本而不显示图像。 Logcat没有显示此更改的错误,只表示" java.lang.Integer无法转换为android.os.Parcelable"。

MainActivity:

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

    gridView = (GridView) findViewById(R.id.gridView);
    gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
    gridView.setAdapter(gridAdapter);

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            ImageItem item = (ImageItem) parent.getItemAtPosition(position);

                Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
                intent.putExtra("title", item.getTitle());
                intent.putExtra("image", item.getImage());

            startActivity(intent);
        }
    });

}


private ArrayList<ImageItem> getData() {
    final ArrayList imageItems = new ArrayList();
    String[] itemsArray = getResources().getStringArray(R.array.grid_ids);

    TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
    for (int i = 0; i < imgs.length(); i++) {
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), imgs.getResourceId(i, -1));
        imageItems.add(new ImageItem(bitmap, itemsArray[i]));
    }
    return imageItems;
}

DetailsActivity:

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

    Intent intent = getIntent();

    String title = getIntent().getStringExtra("title");
    Bitmap image = getIntent().getParcelableExtra("image");

    TextView titleTextView = (TextView) findViewById(R.id.title);
    titleTextView.setText(title);

    ImageView imageView = (ImageView) findViewById(R.id.image);
    imageView.setImageBitmap(image);

ImageItem类:

public class ImageItem {

public Bitmap image;
public String title;

public ImageItem(Bitmap image, String title) {
    super();
    this.image = image;
    this.title = title;
}

public Bitmap getImage() {
    return image;
}

public void setImage(Bitmap image) {
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

GridViewAdapter:

public class GridViewAdapter extends ArrayAdapter<ImageItem> {

private Context context;
private int layoutResourceId;
private ImageView imageView;
private ArrayList<ImageItem> data = new ArrayList<ImageItem>();

public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

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

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = data.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageBitmap(item.getImage());
    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView image;
}

图像(在R.drawable中)和描述在两个字符串数组中的strings.xml中列出。

2 个答案:

答案 0 :(得分:0)

有时,位图可能对于编码和放大而言太大。在intent中解码或传递为字节数组。这可能会导致OOM或糟糕的UI体验。

由于您只使用可绘制文件,而不是位图,您可以在模型类中使用int值:

您可以在模型类中添加drawable id,如下所示:

imageItems.add(new ImageItem(imgs.getResourceId(0,-1),itemsArray [i]));

在适配器中使用它时:

holder.image.setImageResource(imgs.getResourceId(0,-1));

您可以将此int发送到您的详细活动,然后您可以设置:

int imageId = getIntent()。getExtras()。getInt(“image”); imageView.setImageResource(图像标识);

更新了答案

您的模特课程:

公共类ImageItem {

//而不是位图使用int public int image; public String title;

public ImageItem(int image,String title){//这里它将是你的资源id this.image = image; this.title = title; }

public int getImage(){return image; }

public void setImage(int image){this.image = image; }

public String getTitle(){return title; }

public void setTitle(String title){this.title = title; }

}

在您的MainActivity中:

gridView.setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView parent,View v,int position,long id){

    ImageItem item = (ImageItem) parent.getItemAtPosition(position);

        Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
        intent.putExtra("title", item.getTitle());
        intent.putExtra("image", item.getImage());

    startActivity(intent);
}
});

private ArrayList getData(){final ArrayList imageItems = new ArrayList(); String [] itemsArray = getResources()。getStringArray(R.array.grid_ids);

TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
imageItems.add(new ImageItem(imgs.getResourceId(0, -1), itemsArray[i]));
}
return imageItems;

}

在您的DetailsActivity中:

@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.activity_details);

Intent intent = getIntent();

String title = getIntent().getStringExtra("title");
int image = getIntent().getExtras().getInt("image");

TextView titleTextView = (TextView) findViewById(R.id.title);
titleTextView.setText(title);

ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(image);

在您的广告中

holder.image.setImageResource(imgs.getResourceId(0,-1));

答案 1 :(得分:0)

为了让您的实现更容易,您可以让该图像模型对象通过构造函数获得图像资源ID,如下所示。

public class ImageItem {

public int image;
public String title;

public ImageItem(int image, String title) {
    // image means your image resource id
    this.image = image;
    this.title = title;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

在你的onClick方法中你可以像下面那样

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

    ImageItem item = (ImageItem) parent.getItemAtPosition(position);

        Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
        intent.putExtra("title", item.getTitle());
        // your image resource should be int
        intent.putExtra("imageId", item.getImage());

    startActivity(intent);
}

您的DetailsActivity将执行以下操作...

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

    Intent intent = getIntent();

    String title = getIntent().getStringExtra("title");

    // here -1 is the default value for, if that extra isn't found
    int image = getIntent().getIntExtra("imageId", -1);

    TextView titleTextView = (TextView) findViewById(R.id.title);
    titleTextView.setText(title);

    ImageView imageView = (ImageView) findViewById(R.id.image);
    imageView.setImageResource(image);