Note that there are some explanatory texts on larger screens.

plurals
  1. POGet ID of View in GridView
    text
    copied!<p><strong>What I've tried</strong></p> <hr> <p>Hello Guys, I first created a database which I've filled with 3 Collumns, <em>RowID, imageID, text.</em> Afterwards I created a GridView which if filled over my database. Now I have created a onItemClickListner, in which I'd like to get the imageID over the position. After that happend I serach my DB-Table after this imageID, to get the <em>text</em>. This stuff I nneed to save in my other Table. The problem is I think, that I don't get the imageID, and like this serach for nothin in the DB, if you take a look at the Logcat you see that it's null, so it probably didn't get the imageID or I search in my DBAdapter not like I should.</p> <p><strong>Question</strong></p> <hr> <p><strong>IMPORTRANT:</strong> I finally get the ID of the View thx to FuzzialLogic, but there is still a problem, I can't get Row over my Cursor. Please take a look at that!</p> <p>&lt;-----------------------------------------------------WORKS----------------------------------------------------------------> What do I need to change in my Code, that I can get the ID of the Image the User clicked on. Afterwards I'd like to search my DB with that Image-ID, to get the Text which I stored with the Image. This stuff afterwards goes in my other Table.</p> <p>Down here you see the Code and LogCat-Log. I also added my DatabaseAdapter if someone needs it. Thx for your help in Advance!</p> <p><strong>Code</strong></p> <hr> <p><strong>SFilterConfigActivity.class:</strong></p> <pre><code> package de.retowaelchli.filterit; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.EditText; import android.widget.GridView; import android.widget.Toast; import android.*; import de.retowaelchli.filterit.database.SmileyDBAdapter; import de.retowaelchli.filterit.database.SFilterDBAdapter; import de.retowaelchli.filterit.stats.ImageAdapter; public class SFilterConfigActivity extends Activity { //Variablen deklarieren private String name; private String keyword; private String smiley; private String text; private String source; private Integer[] info; private SmileyDBAdapter SmileyHelper; private SFilterDBAdapter mDbHelper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sfilter_config); SmileyHelper = new SmileyDBAdapter(this); mDbHelper = new SFilterDBAdapter(this); getImages(); grid(); } public void grid(){ //Hier wird die GridView definiert und anschliesend über den ImageAdapter gefüllt final GridView gridview = (GridView) findViewById(R.id.SmileyGrind); gridview.setAdapter(new ImageAdapter(this, info)); //Hier wird definiert was passiert wenn auf ein Bild in der GridView geklickt wird gridview.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { Toast.makeText(SFilterConfigActivity.this, "" + position, Toast.LENGTH_SHORT).show(); // gridview.setSelection(position); // Funktioniert noch nich Custom GridView Layout dafür erstellen siehe Stackoverflow if(position == gridview.getSelectedItemPosition()){ v.setBackgroundColor(0xFF00FF00); } else{ v.setBackgroundColor(0x0000000); } //Hier wird herrausgefunden welche ID das Bild hat und in den jeweiligen String gespeichert source = (new Long(id)).toString(); SmileyHelper.open(); Cursor c = SmileyHelper.getSmiley(source); startManagingCursor(c); if (c.moveToFirst()) { do { text = c.getString(c.getColumnIndex(SmileyDBAdapter.SOURCE)); smiley = c.getString(c.getColumnIndex(SmileyDBAdapter.INFO)); } while (c.moveToNext()); SmileyHelper.close(); } } }); } public Integer[] getImages(){ SmileyHelper.open(); Cursor c = SmileyHelper.getAllSmileys(); ArrayList&lt;Integer&gt; infoList = new ArrayList&lt;Integer&gt;(); c.getColumnIndex(SmileyDBAdapter.INFO); int ColumnIndex = c.getColumnIndex(SmileyDBAdapter.INFO); if(c!=null) { while(c.moveToNext()){ String infoItem = c.getString( ColumnIndex ); infoList.add(Integer.parseInt(infoItem)); } } info = infoList.toArray(new Integer[]{}); c.close(); SmileyHelper.close(); return info; } public void onClickSConfigSave(View v){ EditText edtTextName = (EditText)findViewById(R.id.SFConfigName); EditText edtTextKeyword = (EditText)findViewById(R.id.SFConfigKeyword); name = edtTextName.getText().toString(); keyword = edtTextKeyword.getText().toString(); mDbHelper.open(); mDbHelper.createSFilter(name, keyword, smiley, text); mDbHelper.close(); final Intent i = new Intent(this, SmileyActivity.class); startActivity(i); } } </code></pre> <p><strong>ImageAdapter.class</strong></p> <pre><code> package de.retowaelchli.filterit.stats; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; //import de.retowaelchli.filterit.SFilterConfigActivity; public class ImageAdapter extends BaseAdapter { // references to our images private Integer[] mThumbIds; private Context mContext; public ImageAdapter(Context c, Integer[] imageIds) { mContext = c; mThumbIds = imageIds; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return mThumbIds[position]; } public long getItemId(int position) { return position; } // create a new ImageView for each item referenced by the Adapter public View getView(final int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } imageView.setId(mThumbIds[position]); imageView.setImageResource(mThumbIds[position]); return imageView; } } </code></pre> <p>Like this I added the pictures with some text to the DB:</p> <pre><code> private void angry(){ int drawableID = context.getResources().getIdentifier("angry", "drawable", getPackageName()); iv.setImageResource(drawableID); //String info = String.valueOf(drawableID); String info = String.valueOf(drawableID); mDbHelper.open(); mDbHelper.createSmiley("You received a angry message", info); mDbHelper.close(); } </code></pre> <p><strong>Log-Cat</strong></p> <hr> <p>And here's the Log:</p> <pre><code>10-12 11:32:29.632: DEBUG/Database(25130): dbopen(): path = /data/data/de.retowaelchli.filterit/databases/filterit, flag = 6 10-12 11:32:29.632: DEBUG/Database(25130): dbopen(): path = /data/data/de.retowaelchli.filterit/databases/filterit, free size = 663 10-12 11:32:30.612: DEBUG/Database(25130): dbopen(): path = /data/data/de.retowaelchli.filterit/databases/filterit, flag = 6 10-12 11:32:30.612: DEBUG/Database(25130): dbopen(): path = /data/data/de.retowaelchli.filterit/databases/filterit, free size = 663 10-12 11:32:30.632: ERROR/Database(25130): Error inserting text=null smiley=null keyword=test name=test 10-12 11:32:30.632: ERROR/Database(25130): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 10-12 11:32:30.632: ERROR/Database(25130): at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 10-12 11:32:30.632: ERROR/Database(25130): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61) 10-12 11:32:30.632: ERROR/Database(25130): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1671) 10-12 11:32:30.632: ERROR/Database(25130): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1515) 10-12 11:32:30.632: ERROR/Database(25130): at de.retowaelchli.filterit.database.SFilterDBAdapter.createSFilter(SFilterDBAdapter.java:89) 10-12 11:32:30.632: ERROR/Database(25130): at de.retowaelchli.filterit.SFilterConfigActivity.onClickSConfigSave(SFilterConfigActivity.java:143) 10-12 11:32:30.632: ERROR/Database(25130): at java.lang.reflect.Method.invokeNative(Native Method) 10-12 11:32:30.632: ERROR/Database(25130): at java.lang.reflect.Method.invoke(Method.java:507) 10-12 11:32:30.632: ERROR/Database(25130): at android.view.View$1.onClick(View.java:2186) 10-12 11:32:30.632: ERROR/Database(25130): at android.view.View.performClick(View.java:2532) 10-12 11:32:30.632: ERROR/Database(25130): at android.view.View$PerformClick.run(View.java:9277) 10-12 11:32:30.632: ERROR/Database(25130): at android.os.Handler.handleCallback(Handler.java:587) 10-12 11:32:30.632: ERROR/Database(25130): at android.os.Handler.dispatchMessage(Handler.java:92) 10-12 11:32:30.632: ERROR/Database(25130): at android.os.Looper.loop(Looper.java:143) 10-12 11:32:30.632: ERROR/Database(25130): at android.app.ActivityThread.main(ActivityThread.java:4196) 10-12 11:32:30.632: ERROR/Database(25130): at java.lang.reflect.Method.invokeNative(Native Method) 10-12 11:32:30.632: ERROR/Database(25130): at java.lang.reflect.Method.invoke(Method.java:507) 10-12 11:32:30.632: ERROR/Database(25130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-12 11:32:30.632: ERROR/Database(25130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-12 11:32:30.632: ERROR/Database(25130): at dalvik.system.NativeStart.main(Native Method) </code></pre> <hr> <p>Here you find the Code of my SmileyDBAdapter.</p> <p><strong>SmileyDB</strong></p> <pre><code>package de.retowaelchli.filterit.database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SmileyDBAdapter { public static final String ROW_ID = "_id"; public static final String SOURCE = "source"; public static final String INFO = "info"; private static final String DATABASE_TABLE = "smiley"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private final Context mCtx; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } /** * Constructor - takes the context to allow the database to be * opened/created * * @param ctx * the Context within which to work */ public SmileyDBAdapter(Context ctx) { this.mCtx = ctx; } public SmileyDBAdapter open() throws SQLException { this.mDbHelper = new DatabaseHelper(this.mCtx); this.mDb = this.mDbHelper.getWritableDatabase(); return this; } /** * close return type: void */ public void close() { this.mDbHelper.close(); } public long createSmiley(String source, String info ){ ContentValues initialValues = new ContentValues(); initialValues.put(SOURCE, source); initialValues.put(INFO, info); return this.mDb.insert(DATABASE_TABLE, null, initialValues); } public boolean deleteSmiley(long rowId) { return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) &gt; 0; } public Cursor getAllSmileys() { return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID, SOURCE, INFO }, null, null, null, null, null); } //Es wird nach der ID des smiley gesucht. public Cursor getSmiley(String info) throws SQLException { Cursor mCursor = this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, SOURCE, INFO }, INFO + "=" + info, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } public boolean updateSmiley(long rowId, String source, String info, String cache){ ContentValues args = new ContentValues(); args.put(SOURCE, source); args.put(INFO, info); return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) &gt;0; } } </code></pre> <p><strong>DBAdapter</strong></p> <pre><code>package de.retowaelchli.filterit.database; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBAdapter { public static final String DATABASE_NAME = "filterit"; public static final int DATABASE_VERSION = 1; public static final String CREATE_TABLE_ADFILTER = "create table adfilter (_id integer primary key autoincrement, " + ADFilterDBAdapter.NAME+" not null ," + ADFilterDBAdapter.KEYWORD+" not null ," + ADFilterDBAdapter.CACHE + " not null );"; private static final String CREATE_TABLE_SFILTER = "create table sfilter (_id integer primary key autoincrement, " +SFilterDBAdapter.NAME+" not null ," +SFilterDBAdapter.KEYWORD+" not null ," +SFilterDBAdapter.SMILEY+ " not null ," +SFilterDBAdapter.TEXT+ " not null );"; private static final String CREATE_TABLE_ADMESSAGES = "create table admessages (_id integer primary key autoincrement, " +MessagesDBAdapter.PHONENUMBER+" not null ," +MessagesDBAdapter.MESSAGE+ " not null );"; private static final String CREATE_TABLE_SMILEY = " create table smiley (_id integer primary key autoincrement, " +SmileyDBAdapter.SOURCE+" not null ," +SmileyDBAdapter.INFO+ " not null );"; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; /** * Constructor * @param ctx */ public DBAdapter(Context ctx) { this.context = ctx; } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_ADFILTER); db.execSQL(CREATE_TABLE_SFILTER); db.execSQL(CREATE_TABLE_ADMESSAGES); db.execSQL(CREATE_TABLE_SMILEY); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Adding any table mods to this guy here } } /** * open the db * @return this * @throws SQLException * return type: DBAdapter */ public DBAdapter open() throws SQLException { this.DBHelper = new DatabaseHelper(this.context); this.db = this.DBHelper.getWritableDatabase(); return this; } /** * close the db * return type: void */ public void close() { this.DBHelper.close(); } } </code></pre> <hr>
 

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