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);
}
};
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 likereturn 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 nowSecond option (easy way out)
You can put info in the gridviewitem’s view using
setTag()then in
onItemClickyou callgetTag()and there you have your unique id