Reputation: 45
hi i have code for grid view of images and when i click each image of grid it open empty layout but i want when i click the first image so open it with bigger size i mean call the same image that i clicked this is the code of main
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class fitgridview extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fitgridview);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity2.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
and this is the code of myAdapter java class :-
import android.content.Context;
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 java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 06/08/2016.
*/
public class MyAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mItems.add(new Item("Red", R.drawable.ic_menu_camera));
mItems.add(new Item("Magenta", R.drawable.ic_menu_gallery));
mItems.add(new Item("Dark Gray", R.drawable.ic_menu_manage));
mItems.add(new Item("Gray", R.drawable.ic_menu_send));
mItems.add(new Item("Green", R.drawable.ic_menu_share));
mItems.add(new Item("Cyan", R.drawable.ic_menu_slideshow));
mItems.add(new Item("Red", R.drawable.ic_menu_camera));
mItems.add(new Item("Magenta", R.drawable.ic_menu_gallery));
mItems.add(new Item("Dark Gray", R.drawable.ic_menu_manage));
mItems.add(new Item("Gray", R.drawable.ic_menu_send));
mItems.add(new Item("Green", R.drawable.ic_menu_share));
mItems.add(new Item("Cyan", R.drawable.ic_menu_slideshow));
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Item getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return mItems.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private static class Item {
public final String name;
public final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
and this is the SquareImageView java class :-
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by lenovo on 06/08/2016.
*/
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
how can i call each image when i click it ? what should i write in SingleViewActivity2 to do what i wnt help me and thanks alot
Upvotes: 0
Views: 44
Reputation: 191758
Firstly, the adapter only holds a list. You add objects from the Activity and pass in the list there.
In code,
// Fields
private List<Item> mItems = new ArrayList<Item>();
private MyAdapter mAdapter;
// onCreate
mAdapter = new MyAdapter(this, mItems);
gridView.setAdapter(mAdapter);
Now, if you'd instead extend ArrayAdapter<Item>
instead of BaseAdapter
, you could take advantage of some useful methods. For example,
mAdapter.add(new Item(...));
Now, it's recommended you research how to do public class Item implements Parcelable
, which is outside the scope of this answer.
Once you have that, you can pass Parcelable
objects between Activities
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity2.class);
// Pass image item
i.putExtra("item", mAdapter.getItem(position));
startActivity(i);
}
});
Upvotes: 1
Reputation: 331
Pass the object from activity to adapter and then render.
currently you are creating and rendering object from adapter it is an programming practice.
when you pass object from activity then you can pass object with intent to next activity and you will achieve your requirement.
this the only way to your problem.
Upvotes: 0