sqlitelistviewandroid-activitycustom-adapter

How can I display SQLite data using customloader and CustomAdapter?


I want to display sqlite data in listview using CustomAdapter.
The following link helps in displaying only single item in the list.
Please help me to display more than one item in a single row.

http://www.phloxblog.in/?p=1890


Solution

  • First, create new class which extends CursorAdapter and give it a name. This new CursorAdapter class must implement the inherited abstract methods as following:

    public class MyCursorAdapter extends CursorAdapter {
    
        // Default constructor
        public MyCursorAdapter(Context context, Cursor cursor, int flags) {
            ...
        }
    
        public void bindView(View view, Context context, Cursor cursor) {
            ...
        }
    
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            ...
            return null;
        }
    }

    Next define the methods of MyCursorAdapter

    In BaseAdapter, view is created in getView method; in CursorAdapter, however, view is created in newView() method and elements are populated in bindView(). In the newView() method, you simply inflate the view your custom xml and return it. In the bindView() method, you set the elements of your view. Here is code:

    public void bindView(View view, Context context, Cursor cursor) {
           TextView textViewTitle = (TextView) view.findViewById(R.id.articleTitle);
           String title = cursor.getString( cursor.getColumnIndex( MyTable.COLUMN_TITLE ) )
           textViewTitle.setText(title);
           ...
        }
    
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // R.layout.list_row is your xml layout for each row
            return cursorInflater.inflate(R.layout.list_row, parent, false);
        }
    }

    Then in the Activity,Since loading data from database is heavy-duty job, we will load the data in the Thread. If you do not explicitly start CursorAdapter in its own thread then it will run on the main (UI) thread which may be noticeable as jittery or slow to respond interface by your users. Here we'll use android Handler

    public class ActivitySample extends Activity {
        MyCursorAdapter customAdapter;
        private Cursor mCursor;
        private ListView listView;
    
        // Default constructor
        public onCreate(Bundle savedInstanceState) {
            ...
            listView = (ListView) findViewById(R.id.activity_sample_layout);
    
            // Your database schema
            String[] mProjection = { 
                    MyTable.COLUMN_ID, 
                    MyTable.COLUMN_TITLE,
            };
    
            // Here we query database
            mCursor =  getContentResolver().query(
                    MyAdContentProvider.CONTENT_URI,
                    mProjection, 
                    null,   
                    null,                                       
                    null);
    
    
            listView.setOnItemClickListener(new OnItemClickListener() {
                    ...
            }):
        }
    
        new Handler().post(new Runnable() {
    
            @Override
            public void run() {
                customAdapter = new MyCursorAdapter(
                    ActivitySample.this, 
                    mCursor,
                    0);
    
                listView.setAdapter(customAdapter);
            }
    
        });
    
    }