Search code examples
androidandroid-listviewcursorsimplecursoradapter

Android SimpleCursorAdapter and CursorLoader


I have a android list layout. Basically it will list user's info on the screen. Please help me explain how the listView set the data. How the SimpleCursorAdapter links with Loader

Here's code :

public class ChatList extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>{

    private SimpleCursorAdapter adapter;
    private final int Adapter_AccountName = 1;  
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_chatlist);   

        adapter = new SimpleCursorAdapter(this, 
                R.layout.main_list_item, 
                null, 
                new String[]{DataProvider.COL_NAME, DataProvider.COL_COUNT,DataProvider.PROFILE_COL_LASTMSGAT,DataProvider.PROFILE_COL_IMAGE},  
                new int[]{R.id.text1, R.id.text2,R.id.text3,R.id.avatar},
                0);

        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                switch(view.getId()) {
                // here can add one more line in the main page for each account
                case R.id.text2:
                    int count = cursor.getInt(columnIndex);
                    if (count > 0) {
                        ((TextView)view).setText(String.format("%d new message%s", count, count==1 ? "" : "s"));
                    }
                    return true;    
                case R.id.text3: 
                    String lastUpdate = cursor.getString(columnIndex);
                    Date d = DbDatetimeUtility.getDate(cursor.getString(columnIndex));  
                    Date t = DbDatetimeUtility.getCurrentDate();
                    ((TextView)view).setText(DbDatetimeUtility.returnDifferentTime(d,t)); 
                    return true;    
                case R.id.avatar:
                    byte[] imageByte = cursor.getBlob(columnIndex);
                    ((ImageView)view).setImageBitmap(DbBitmapUtility.getResizedBitmap(DbBitmapUtility.getImage(imageByte),125,125));
                    return true;
                }
                return false;
            }
        });

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);

        //final ListView listView = getListView();
        final ListView listView = getListView(); 

        listView.setAdapter(adapter);  

        getLoaderManager().initLoader(0, null, this);  

    }   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {   

        case R.id.action_settings:
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true; 
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra(Common.PROFILE_ID, String.valueOf(id));
        startActivity(intent);
    }   

    //----------------------------------------------------------------------------

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader loader = new CursorLoader(this, 
                DataProvider.CONTENT_URI_PROFILE, 
                new String[]{DataProvider.COL_ID, DataProvider.COL_NAME, DataProvider.COL_COUNT,DataProvider.PROFILE_COL_LASTMSGAT,DataProvider.PROFILE_COL_IMAGE}, 
                null, 
                null,//new String[]{DataProvider.PROFILE_COL_LASTMSGAT},  
                DataProvider.PROFILE_COL_LASTMSGAT + " DESC"); 
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }

}

Thanks in advance!


Solution

  • Briefly for now, the main points are:

    1. onCreateLoader gets the data from the SQLite database.
    2. This code adapter = new SimpleCursorAdapter, populates the adapter.
    3. This code listView.setAdapter(adapter); populates the ListView.

    There is a nice Stackoverflow answer at Using SimpleCursorAdapter to get Data from Database to ListView