Skip to content Skip to sidebar Skip to footer

Get Id Of View In Gridview

What I've tried Hello Guys, I first created a database which I've filled with 3 Collumns, RowID, imageID, text. Afterwards I created a GridView which if filled over my database. N

Solution 1:

Safari,

After looking over your code and analyzing what could be causing the error, I've noticed a few things. The issue is that your code is trying to interchange the rowID and viewID, but that is not actually the desired behavior.

In OnItemClick(), the id argument refers to rowID. You don't actually have a rowID that is guaranteed (by the code) to be unique, so it is failing. While you could "force the issue" via getItemId(), that function merely provides a pointer to an object (Integer). Ignore getItemId() as this is only causing you pain. Instead, grab the view itself, and get the viewId directly from it, like so:

EDITsource = v.getId().toString();Reason: Since your TABLE doesn't declare a Type, it is most likely going in as a String. We know the ImageID is now returning, and we know the data is there... So that leaves type conversion, so far as I can tell.

parent and view often cause confusion, particularly with convoluted nested layouts like GridViews and ListViews. However, your GridView is quite simple and v should refer to the ImageView that is stored there. If the ImageView was stored in a LinearLayout or other similar parent, there might be an issue, but such is not the case for you.

This will bypass your need to worry about the getItemId(int position), as again, this refers to rowId and only works if you can guarantee to the Adapter certain things, specifically a unique rowId for each item.

Second, your viewId is set to a pointer for an Integer and not the value itself. An int is a primitive, so assignment happens as expected. An Integer is an Object so assignment happens to the address of the object, not the object itself. I would suggest changing imageView.setId(mThumbIds[position]); (in your getView()) to:

   imageView.setId(mThumbIds[position].intValue());

This will get the primitive int value instead of a reference to an Integer object in memory. Unless something else errant is happening in your code (and I don't see anything right offhand), this should resolve your issue. setId is meant to fail quietly if an inappropriate id is assigned to it, so it may not be working and simply not telling you.

Finally, I can't see what type of field the 'SOURCE' is in your code. It doesn't show the actual table creation. So, further guessing at your particular database is impossible. Hope this helps!

Regarding your Database The main thing that stands out about your Database is your CREATE TABLE statement for your Simleys Table. In SQLite, if you do not explicitly assign a Type, SQLite will determine the Type each time you make a query, and return the type according to what it believes is in the field. This means that numbers may be interpreted as String and vice versa. In general, it can be pretty reliable, however, it can lead to pitfalls.

One of the biggest issues in code is assuming the code is doing what you want it to. In fact, you can only guarantee this if you tell it exactly what to do.

Second, I noticed that when you get the images to build your Array for your ImageAdapter that you are doing a query on the INFO field, not the SOURCE. This is misleading in your code as you tell the onItemClick that you are getting the SOURCE. Consider changing this variable name.

Next, there is a dysfunction when querying the INFO field via getSmiley(String info). It is expecting a String and not an Integer according to your arguments. Adding a String to a ContentValues object is different than adding an Integer. A String gets added with '' surrounding it, separating '1' from 1. This means that all of your Integers were INSERTED as String and when you are querying you are not including the necessary ''. So a number of changes is advised.

  • First, adjust your CREATE TABLE Statement.

    private static final String CREATE_TABLE_SMILEY = " create table smiley (_id integer primary key autoincrement, " +SmileyDBAdapter.SOURCE+" not null ," +SmileyDBAdapter.INFO+ " integer not null );";

Changing the statement in this way will ensure that you are getting an integer and not a string.

  • Next, change your getImages() in the following manner: String infoItem = c.getString( ColumnIndex ); to int infoItem = c.getInt( ColumnIndex );

  • Next, change your createSmiley(String source, String info) to createSmiley(String source, int info) . This should adjust all of your inserts correctly. Your ContentValues object (named initialValues) will know what to do.

  • Next, change your getSmiley(String info) to getSmiley(int info). This should adjust your query correctly as well. If it does not, change ... + "=" + info + ... to ... + "=" + info.toString() + .... This will force the issue for you.

  • Next, make your onItemClick() match by changing this line source = v.getId().toString(); back to source = v.getId();

  • Finally, you will have to change any of your calls to createSmiley() to send the int value, not the value converted to a String.

This is a lot of changes (not really) to make. So, please back up your source files before you make these changes. Then its just a matter of uninstalling the package (including from the emulator, if it is emulated) and reinstalling it, so it doesn't keep the original data (with the Strings and not the Integers).

FuzzicalLogic

Solution 2:

In Your ImageAdapter code.You have overridden method getItemid ther you are returning 0 that is causing problem.try returning mThumbIds[position] i think this will do the trick for you.

cheers.

Solution 3:

In ImageAdapter class in getView method add the following line of code

imageView.setId(mThumbIds[postion]);

or another option is in your Onclick listener of gridview inside on click method

source = (new Long(id)).toString();

replace this line of code with

source = parent.getItemId(position);

Now this will definately solve the issue.

Solution 4:

change

if(c!=null)
    {

       while(c.moveToNext()){
           String infoItem = c.getString( ColumnIndex );
           infoList.add(Integer.parseInt(infoItem));
       }
    }

to

if(c!=null)
        {

           while(!c.isAfterLast()){
               String infoItem = c.getString( ColumnIndex );
               infoList.add(Integer.parseInt(infoItem));
               c.moveToNext();
           }
        }

Post a Comment for "Get Id Of View In Gridview"