2

I am fetching data from an sqlite database to Android mobile in a listview. The data comes one by one in the list. I will display two textview in custom listview using cursor adapter. Main.java

public class Main extends Activity  {

    private SQLiteAdapter mySQLiteAdapter;
    /** Called when the activity is first created. */


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ListView listContent = (ListView) findViewById(R.id.customlist);

            /*
             * Create/Open a SQLite database and fill with dummy content and close
             * it
             */
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToWrite();
            // mySQLiteAdapter.deleteAll();
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToRead();

            final Cursor cursor = mySQLiteAdapter.queueAll();
            startManagingCursor(cursor);

            String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
            int[] to = new int[] { R.id.text };

            final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                    R.layout.row, cursor, from, to);

            listContent.setAdapter(cursorAdapter);

            mySQLiteAdapter.close();
            }
    }

SQLiteAdapter.java

public class SQLiteAdapter {

    public static final String MYDATABASE_NAME = "test";
    public static final String MYDATABASE_TABLE = "test";
    public static final int MYDATABASE_VERSION = 1;     
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT = "test"


    // create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE = "create table test (_id integer primary key autoincrement, "
             + "test text not null)";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c) {
        context = c;
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public SQLiteAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        sqLiteHelper.close();
    }


    public int deleteAll() {
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public Cursor queueAll() {
        String[] columns = new String[] { KEY_ID, KEY_CONTENT };
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
                null, null, null, null);

        int id=cursor.getColumnIndex(KEY_CONTENT);
        cursor.moveToFirst();
        if(cursor!=null)
        {
            do      
            {
                String keyid=cursor.getString(id);
                System.out.println(keyid);
            }while(cursor.moveToNext());
        }

        return cursor;
    }

    private static class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }
    }
    }

Using the above code I can display key_content in listview. Now I will display key_id, and key_content both in listview using cursor adapter.

1 Answer 1

1
 Cursor cursor = mySQLiteAdapter.queueAll();

in queueAll() method

 Cursror c = sqLiteDatabase.rawQuery("select * from TABLENAME",null);

 return c;

instead of * you can add your table column name like key_id,key_content

And get id and name

String myContent = cursor.getString(cursor.getColumnIndex("key_content")).toString().trim();

    long id = cursor.getLong(cursor.getColumnIndex("key_id"));
Sign up to request clarification or add additional context in comments.

3 Comments

ok.. in int[] to = new int[] { R.id.text }; final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); the above line row.xml i can created text view i displayed as listview like that i will display along with key_content and key_id
the above code i can pass only key_content in the listview now i want pass key_id also in listview..please tell me how to do that
you have just pass cursor above my code cursor.and in your adapter check my get id and name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.