Reputation: 617
I have the following code. When I click on an image in grid view that particular image should display in another activity. How can I do that?
This is my Activity class::
public class ProductsActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
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)
{
// this 'mActivity' parameter is Activity object, you can send the current activity.
Intent i = new Intent(getApplicationContext(), MobileDisplayActivity.class);
i.putExtra("positionId",position);
startActivity(i);
}
});
}
How do I can change my adapter class to achieve my requirement. Ithink the problem is in getView()
.
this is my adapter class:
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("Image 1", R.drawable.ic_image4));
items.add(new Item("Image 2", R.drawable.ic_image5));
items.add(new Item("Image 3", R.drawable.ic_image4));
items.add(new Item("Image 4", R.drawable.ic_image5));
items.add(new Item("Image 5", R.drawable.ic_image4));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.row_grid, 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 = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}
Upvotes: 0
Views: 71
Reputation: 18112
Rather than sending the position of the item to the next activity, you could send the id or your drawables (which is also integer).
Do it this way
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// this 'mActivity' parameter is Activity object, you can send the current activity.
int imageId = ((Item) parent.getItemAtPosition(position)).drawableId;
Intent i = new Intent(getApplicationContext(), MobileDisplayActivity.class);
i.putExtra("imageId", imageId);
startActivity(i);
}
In the MobileDisplayActivity
activity simple do this
int imageId = getIntent().getIntExtra("imageId", 0);
if(imageId > 0)
imageView.setBackgroundResource(imageId);
else
// something went wrong
NOTE: Make Item
class public static or define it independently.
Upvotes: 1
Reputation: 1583
You should put in the intent the drawable id like in the code below:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int drawableId = ((Item) parent.getItemAtPosition(position)).drawableId;
Intent intent = new Intent(ProductsActivity.this, MobileDisplayActivity.class);
intent.putExtra("DRAWABLE_ID", drawableId);
startActivity(intent);
}
Also you should make the Item class public static in order to have access to it from activity. drawableId should be public (or you could create a getter for this field).
Upvotes: 1