Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Safari,</p> <p>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.</p> <p>In <code>OnItemClick()</code>, the <code>id</code> argument refers to <code>rowID</code>. You don't actually have a <code>rowID</code> that is guaranteed (by the code) to be unique, so it is failing. While you could "force the issue" via <code>getItemId()</code>, that function merely provides a pointer to an object (Integer). Ignore <code>getItemId()</code> as this is only causing you pain. Instead, grab the view itself, and get the <code>viewId</code> directly from it, like so:</p> <p><em><strong>EDIT</em></strong> <code>source = v.getId().toString();</code> <em>Reason:</em> 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.</p> <p><code>parent</code> and <code>view</code> often cause confusion, particularly with convoluted nested layouts like <code>GridViews</code> and <code>ListViews</code>. However, your <code>GridView</code> is quite simple and <code>v</code> should refer to the <code>ImageView</code> that is stored there. If the <code>ImageView</code> was stored in a <code>LinearLayout</code> or other similar parent, there might be an issue, but such is not the case for you.</p> <p>This will bypass your need to worry about the <code>getItemId(int position)</code>, as again, this refers to <code>rowId</code> and only works if you can guarantee to the <code>Adapter</code> certain things, specifically a unique <code>rowId</code> for each item.</p> <p>Second, your <code>viewId</code> is set to a pointer for an <code>Integer</code> and not the value itself. An <code>int</code> is a primitive, so assignment happens as expected. An <code>Integer</code> is an <code>Object</code> so assignment happens to the address of the object, not the object itself. I would suggest changing <code>imageView.setId(mThumbIds[position]);</code> (in your <code>getView()</code>) to:</p> <pre><code> imageView.setId(mThumbIds[position].intValue()); </code></pre> <p>This will get the primitive <code>int</code> value instead of a reference to an <code>Integer</code> 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. <code>setId</code> is meant to fail quietly if an inappropriate <code>id</code> is assigned to it, so it may not be working and simply not telling you.</p> <p>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!</p> <p><em><strong>Regarding your Database</em></strong> 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.</p> <p>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. </p> <p>Second, I noticed that when you get the images to build your <code>Array</code> for your <code>ImageAdapter</code> that you are doing a query on the <code>INFO</code> field, not the <code>SOURCE</code>. This is misleading in your code as you tell the <code>onItemClick</code> that you are getting the <code>SOURCE</code>. Consider changing this variable name.</p> <p>Next, there is a dysfunction when querying the <code>INFO</code> field via <code>getSmiley(String info)</code>. It is expecting a String and not an Integer according to your arguments. Adding a <code>String</code> to a <code>ContentValues</code> object is different than adding an Integer. A <code>String</code> gets added with '' surrounding it, separating '1' from 1. This means that all of your <code>Integers</code> were INSERTED as <code>String</code> and when you are querying you are not including the necessary ''. So a number of changes is advised.</p> <ul> <li><p>First, adjust your CREATE TABLE Statement.</p> <p>private static final String CREATE_TABLE_SMILEY = " create table smiley (_id integer primary key autoincrement, " +SmileyDBAdapter.SOURCE+" not null ," +SmileyDBAdapter.INFO+ " integer not null );";</p></li> </ul> <p>Changing the statement in this way will ensure that you are getting an integer and not a string. </p> <ul> <li><p>Next, change your <code>getImages()</code> in the following manner: <code>String infoItem = c.getString( ColumnIndex );</code> to <code>int infoItem = c.getInt( ColumnIndex );</code></p></li> <li><p>Next, change your <code>createSmiley(String source, String info)</code> to <code>createSmiley(String source, int info)</code> . This should adjust all of your inserts correctly. Your <code>ContentValues</code> object (named <code>initialValues</code>) will know what to do.</p></li> <li><p>Next, change your <code>getSmiley(String info)</code> to <code>getSmiley(int info)</code>. This should adjust your query correctly as well. If it does not, change <code>... + "=" + info + ...</code> to <code>... + "=" + info.toString() + ...</code>. This will force the issue for you.</p></li> <li><p>Next, make your <code>onItemClick()</code> match by changing this line <code>source = v.getId().toString();</code> back to <code>source = v.getId();</code></p></li> <li><p>Finally, you will have to change any of your calls to <code>createSmiley()</code> to send the int value, not the value converted to a String.</p></li> </ul> <p>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).</p> <p>FuzzicalLogic</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload