brahmy adigopula February 2016

how to display position based image for following code?

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(in        

Answers


David Rauca February 2016

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).


Rohit5k2 February 2016

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.

Post Status

Asked in February 2016
Viewed 3,163 times
Voted 10
Answered 2 times

Search




Leave an answer