List Adapter Using SimpleCursorAdapter

This is a discussion on List Adapter Using SimpleCursorAdapter within the Android Forum forums, part of the Android Discussions category; hi, I am new at android. and I am trying to use content provider which uses sqlite and shows the data on the listview using ...

Results 1 to 3 of 3

Thread: List Adapter Using SimpleCursorAdapter

  1. #1
    Android Lurker hamzafar's Avatar
    Join Date
    Oct 2012
    Posts
    1

    List Adapter Using SimpleCursorAdapter

    hi, I am new at android. and I am trying to use content provider which uses sqlite and shows the data on the listview using simplecursor adapter.here is code:

    SqliteOpenHelper::
    public class Sqliteopebhelper extends SQLiteOpenHelper {
    public static final String tag = "OpenHelperClass";


    public static final String DB_NAME = "employee.db";
    public static final String DB_TABLE = "employee";
    public static final String COL_NAME = "empname";
    public static final String COL_PNO = "empno";
    public static final String COL_DESIGNATION = "empdesignation";
    public static final String COL_ADDRESS = "empaddress";
    public static final int DB_VERSION = 1;


    public static final Uri CONTENT_URI = Uri.parse("content://employees");


    static final String CREATE_DATABASE_TABLE_1 = "create table " + DB_TABLE
    + " (" + BaseColumns._ID + " integer primary key autoincrement, "
    + COL_PNO + " text not null, " + COL_NAME + " text not null, "
    + COL_DESIGNATION + " text not null, " + COL_ADDRESS
    + " text not null);";


    public Sqliteopebhelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);


    }


    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_DATABASE_TABLE_1);
    Log.d(tag, CREATE_DATABASE_TABLE_1);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion >= newVersion)
    return;
    String sql = null;
    if (oldVersion == 1)
    sql = "alter table " + DB_TABLE + " add note text;";
    if (oldVersion == 2)
    sql = "";
    Log.d("EventsData", "onUpgrade : " + sql);
    if (sql != null)
    db.execSQL(sql);


    }

    Content Provider::
    public class EmployeesContentProvider extends ContentProvider {


    Sqliteopebhelper db;
    public static final String tag = EmployeesContentProvider.class
    .getSimpleName();


    private static final String AUTHORITY = "employee";
    public static final int TUTORIALS = 100;
    public static final int TUTORIAL_ID = 110;
    private static final String TUTORIALS_BASE_PATH = "tutorials";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
    + "/" + TUTORIALS_BASE_PATH);
    public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
    + "/mt-tutorial";
    public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
    + "/mt-tutorial";


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
    }


    @Override
    public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
    }


    @Override
    public Uri insert(Uri uri, ContentValues values) {
    /*
    * int match = 1; // not the Uri we're expecting long newID = 0; if
    * (match != 1) throw new IllegalArgumentException("Wrong URi " +
    * uri.toString()); if (values != null) { newID =
    * db.getWritableDatabase().insert(Sqliteopebhelper.D B_NAME,
    * Sqliteopebhelper.DB_TABLE, values); return Uri.withAppendedPath(uri,
    * String.valueOf(newID));
    *
    * } else
    */
    return null;
    }


    @Override
    public boolean onCreate() {


    // this is created and is used as for reference for database
    db = new Sqliteopebhelper(this.getContext());
    if (db == null)
    return false;
    else
    Log.d(tag, "OnCretae is True");


    return true;
    }


    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
    String[] selectionArgs, String sortOrder) {


    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(Sqliteopebhelper.DB_TABLE);
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case TUTORIAL_ID:
    queryBuilder.appendWhere(Sqliteopebhelper.COL_PNO + "="
    + uri.getLastPathSegment());
    break;
    case TUTORIALS:
    // no filter
    break;
    default:
    throw new IllegalArgumentException("Unknown URI");
    }
    Cursor cursor = queryBuilder.query(db.getReadableDatabase(),
    projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentR esolver(), uri);
    return cursor;
    }


    @Override
    public int update(Uri uri, ContentValues values, String selection,
    String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
    }


    private static final UriMatcher sURIMatcher = new UriMatcher(
    UriMatcher.NO_MATCH);
    static {
    sURIMatcher.addURI(AUTHORITY, TUTORIALS_BASE_PATH, TUTORIALS);
    sURIMatcher.addURI(AUTHORITY, TUTORIALS_BASE_PATH + "/#", TUTORIAL_ID);
    }
    and Finaly, main activity for listview::
    public class MainActivity extends Activity implements LoaderCallbacks<Cursor> {
    Sqliteopebhelper sqh;
    ListView listView1;
    public static final String tag = MainActivity.class.getSimpleName();
    CursorAdapter mAdapter;
    Cursor cursor;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView1 = (ListView) findViewById(R.id.listView1);
    String[] uiBindFrom = { Sqliteopebhelper.COL_PNO };
    int[] uiBindTo = { R.id.name_entry };
    getLoaderManager().initLoader(0, null, this);
    mAdapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_list_item_1, null, uiBindFrom,
    uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    listView1.setAdapter(mAdapter);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {


    String[] projection = { Sqliteopebhelper.COL_PNO,
    Sqliteopebhelper.COL_NAME };
    CursorLoader cursorLoader = new CursorLoader(this,
    EmployeesContentProvider.CONTENT_URI, projection, null, null,
    null);
    return cursorLoader;
    }


    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    mAdapter.swapCursor(cursor);


    }


    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
    mAdapter.swapCursor(null);


    }


    --------------
    using above code i am not able to show the listView with the items..
    please help me..

  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

  3. #2
    Super Moderator cali's Avatar
    Join Date
    Sep 2011
    Posts
    4,830
    Thanked
    329 times
    Welcome to the site.

  4. #3
    App Developer alostpacket's Avatar
    Join Date
    Jan 2011
    Location
    NY
    Posts
    240
    Thanked
    32 times
    Twitter
    alostpacket
    What Cursor Loader are you using? what "doesnt work"? Can you post a stack trace?

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. Magicsim Dual Sim Adapter - Samsung Galaxy S3
    By Barryj in forum Samsung Galaxy S3
    Replies: 1
    Last Post: 10-05-2012, 06:40 PM
  2. Replies: 0
    Last Post: 06-08-2012, 12:54 PM
  3. Any London Dual Sim Adapter Users Around?
    By cnlon in forum Samsung Galaxy Note
    Replies: 0
    Last Post: 04-09-2012, 04:29 PM
  4. External microphone adapter compatability
    By dkfrey in forum LG Ally Help
    Replies: 1
    Last Post: 08-29-2011, 10:28 AM
  5. USB Host Adapter Arrives for Samsung Galaxy Tab 10.1
    By dgstorm in forum Android News
    Replies: 1
    Last Post: 06-16-2011, 02:19 PM

Search tags for this page

adapter using list<>
,
android cursoradapter delete
,
android cursorloader listadapter
,
android cursorloader simplecursoradapter
,
android listview cursor adapter tutorial
,
android listview cursorloader adapter
,
android simplecursoradapter listview
,
android sqllite update get new cursor listview simplecursoradapter
,
content
,
listview cursoradapter tutorial android
,
listview simplecursoradapter
,
loadercallbacks baseexpandablelistview
,
search listview using simplecursoradapter android
,

simplecursoradapter

,
simplecursoradapter using a list
Click on a term to search our sites for related topics.
Android Forum