0

I'm completely new in using sqlite databases with android. I want to get value from database using following SQL statement:

"SELECT description FROM games WHERE _id =" + categoryID

Where categoryID is the position of clicked element in listView (which is also the value of _id column).

listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,

                String categoryID = new Integer(position).toString();

                final String SELECT_FROM = "SELECT description " + "FROM games "
                        + "WHERE _id = " + categoryID;
                database.query(SELECT_FROM);

                Intent intent = new Intent();
                intent.setClass(SqliteDbActivity.this, ViewAction.class);

                Bundle b = new Bundle();
                b.putString("categoryID", SELECT_FROM);
                intent.putExtras(b);

                startActivity(intent);

            }
        });

I know that I should use database.query() but how should I correctly write the previous sql statement to get value?

Table looks like:

_id description name
1    some1       name1
2    some2       name2       
3    some3       name3    

Please, help.

2
  • What sort of Adapter are you using for your ListView? Commented Apr 9, 2012 at 19:18
  • @MisterSquonk I'm using ArrayAdapter<String> Commented Apr 9, 2012 at 19:19

1 Answer 1

2
String categoryID = new Integer(position).toString();

            final String SELECT_FROM = "SELECT description " + "FROM games "
                    + "WHERE _id = " + categoryID;
            Cursor cursor = database.query(SELECT_FROM);
            cursor.moveToFirst();

            Intent intent = new Intent();
            intent.setClass(SqliteDbActivity.this, ViewAction.class);

            Bundle b = new Bundle();
            b.putString("categoryID", cursor.getString(cursor.getColumnIndex("fieldname"));
            intent.putExtras(b);

            startActivity(intent);

your database.query(SELECT_FROM) will return cursor, with the help of cursor you can do your work

Sign up to request clarification or add additional context in comments.

10 Comments

Thaks, but can I pass this value to the next activity using bundle?
Ok, what should be instead of "fieldname"?
index either 0,1,2... depends on your number of column in your table. cursor.getString(0) will return 1st column value.
So if I have table as at the question index will be 1? Or I misunderstood you.
0 will be your index cursor.getString(0), not 1
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.