1

That GridView adapter creates ImageView from a layout. All images are downloaded from URLs respect to the database item IDs where the ID is got from a JSONArray. Let say, the view is now showing items with ID: 1,3,4,7. As the GridView items are dynamic, the position (starting from 0) cannot really identify my item on the GridView. Is there any other ways to identify that image from the database item IDs?

public OnItemClickListener ClickListner = new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(getApplicationContext(), 
        //      ""+position, Toast.LENGTH_SHORT).show();

        //Identification code for the item to be added here

        Intent view = 
                new Intent(main.this, View.class);
        view.putExtra("ID", id); 
        //expected to have an ID equal to database item ID
        startActivity(view);
    }

};

2 Answers 2

6

First option (the nice way)

You keep a reference to your adapter (or you get it by calling parent.getAdapter() and then cast it)

In your adapter make sure that you've overridden getItem(position) to return the object you used to fill up your adapter (probably something like return arrayList.getItem(position)if you used BaseAdapter)

On the adapter you call getItem(position) and this will give you the very same object, so you should have all the info you need now

Second option (easy way out)

You can put info in the gridviewitem's view using setTag() then in onItemClick you call getTag() and there you have your unique id

5
  • That's great. guy. I successfully passed the ID to my next Activity with the second option you mentioned. However, do you mean that all information other than only the ID can be passed to the next Activity by putExtra("info",getItem(position))? But i think it is better practice to get other information after the user clicked on the item, which query the database again for the detailed information.
    – Henry
    Jun 2, 2012 at 11:59
  • It is possible, but for these kind of things I think it's best to look at the case by case. But do have a look at getSerializableExtra() because you'll definitely run into cases where it WILL be better practice Jun 2, 2012 at 22:33
  • how different getSerializableExtra() compared with getExtra()? I knew that Serializable Data is used for input/output stream. By the way, I can now also add additional info using setExtra() even with getTag() as my JSONArary is saved as a variable for the Activity.
    – Henry
    Jun 3, 2012 at 2:17
  • you can put any object (that is serializable) as an extra with putExtra and then you call getSerializableExtra to get it back Jun 3, 2012 at 12:07
  • so what you mean is that it saves memory space by using serialized data?
    – Henry
    Jun 4, 2012 at 11:34
1

You can use a POJO class to set the URL and ID of Image in that class and create and ArrayList for the same and passing that to the Adapter class. By, doing this you will bind your ImageView and the ImageID from your database. And, then inside onItemClick() you can simply use

 POJO pojo = listview.getAdapter().getitem(position);
 int id = pojo.getId();
2
  • POJO class is fully new to me. I would try if I have time. Anyway, thanks.
    – Henry
    Jun 2, 2012 at 12:04
  • Its nothing just a setter getter class. Jun 2, 2012 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.