views:

30

answers:

0

Hello there, I want to populate ExpandableListView from database. But I can't create the DBAdapter object in class extends BaseExpandableListAdapter, because I don't know what context must be passed to DBAdapter constructor. I use DBAdapter db = new DBAdapter(AutoContacts.this); but it doesn't work. Here my code: Autocontact.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class AutoContacts extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ExpandableListAdapter mAdapter;
        ExpandableListView templateList = (ExpandableListView)findViewById(R.id.ListView01);
        mAdapter = new MyExpandableListAdapter();
        templateList.setAdapter(mAdapter);              

    }

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        private DBAdapter db = new DBAdapter(AutoContacts.this);  //Problem is here         
//Here, I plan to add code to initialize the groups and children Strings from the cursor

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition){
            return childPosition;
        }

        public int getChildrenCount(int groupPosition){
            int i = 0;
            try {
                i = children[groupPosition].length;
            }catch (Exception e){               
            }

            return i;
        }

        public TextView getGenericView() {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);

            TextView textView = new TextView(AutoContacts.this);
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
            textView.setTextColor(Color.RED);
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View converView, ViewGroup parent){
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }

        public Object getGroup(int groupPosition){
            return groups[groupPosition];
        }

        public int getGroupCount(){
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View converView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition){
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.add:
                Intent intent = new Intent(AutoContacts.this, AddUseCases.class);
                startActivity(intent);
                return true;
            default:
                return false;
        }
    }    
}

DBAdapter.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TEMPLATE_NAME = "template_name";
    public static final String KEY_NUMBER_OF_CONTACTS = "number_of_contacts";
    public static final String KEY_FIRST_NAME = "first_name";
    public static final String KEY_LAST_NAME = "last_name";
    public static final String KEY_MIDDLE_NAME = "middle_name";
    public static final String KEY_JOB_TITLE = "job_title";
    public static final String KEY_HOME_PHONE_1 = "home_phone_1";
    public static final String KEY_HOME_PHONE_2 = "home_phone_2";
    public static final String KEY_MOBILE_PHONE_1 = "mobile_phone_1";
    public static final String KEY_MOBILE_PHONE_2 = "mobile_phone_2";   
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "database";
    private static final String DATABASE_TABLE = "templates";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table templates (_id integer primary key autoincrement, "
        + "template_name text not null, "
        + "number_of_contacts integer not null, "
        + "first_name text not null, "
        + "last_name text not null, "
        + "middle_name text not null, "
        + "job_title text not null, "
        + "home_phone_1 text not null, "
        + "home_phone_2 text not null, "
        + "mobile_phone_1 text not null, "
        + "mobile_phone_2 text not null);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

    public DBAdapter open() throws SQLException{
        db = DBHelper.getWritableDatabase();
        return this;
    }

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

    public long insertTitle(String template_name,
                            int number_of_contacts,
                            String first_name,
                            String last_name,
                            String middle_name,
                            String job_title,
                            String home_phone_1,
                            String home_phone_2,
                            String mobile_phone_1,
                            String mobile_phone_2){
        ContentValues values = new ContentValues();
        values.put(KEY_TEMPLATE_NAME, template_name);
        values.put(KEY_NUMBER_OF_CONTACTS, number_of_contacts);
        values.put(KEY_FIRST_NAME, first_name);
        values.put(KEY_LAST_NAME, last_name);
        values.put(KEY_MIDDLE_NAME, middle_name);
        values.put(KEY_JOB_TITLE, job_title);
        values.put(KEY_HOME_PHONE_1, home_phone_1);
        values.put(KEY_HOME_PHONE_2, home_phone_2);
        values.put(KEY_MOBILE_PHONE_1, mobile_phone_1);
        values.put(KEY_MOBILE_PHONE_2, mobile_phone_2);
        return db.insert(DATABASE_TABLE, null, values);
    }

    public boolean deleteTitle(long rowId) {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor getAllTitles() {
        return db.query(DATABASE_TABLE, new String [] {
                        KEY_ROWID,
                        KEY_TEMPLATE_NAME,
                        KEY_NUMBER_OF_CONTACTS,
                        KEY_FIRST_NAME,
                        KEY_LAST_NAME,
                        KEY_MIDDLE_NAME,
                        KEY_JOB_TITLE,
                        KEY_HOME_PHONE_1,
                        KEY_HOME_PHONE_2,
                        KEY_MOBILE_PHONE_1,
                        KEY_MOBILE_PHONE_2},
                        null,
                        null,
                        null,
                        null,
                        null);
    }

    public Cursor getTitle(long rowId) throws SQLException {
        Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {
                     KEY_ROWID,
                     KEY_TEMPLATE_NAME,
                     KEY_NUMBER_OF_CONTACTS,
                     KEY_FIRST_NAME,
                     KEY_LAST_NAME,
                     KEY_MIDDLE_NAME,
                     KEY_JOB_TITLE,
                     KEY_HOME_PHONE_1,
                     KEY_HOME_PHONE_2,
                     KEY_MOBILE_PHONE_1,
                     KEY_MOBILE_PHONE_2},
                     KEY_ROWID + "=" + rowId,
                     null,
                     null,
                     null,
                     null,
                     null);
        if(mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public boolean updateTitle (long rowId, 
                                String template_name,
                                int number_of_contacts,
                                String first_name,
                                String last_name,
                                String middle_name,
                                String job_title,
                                String home_phone_1,
                                String home_phone_2,
                                String mobile_phone_1,
                                String mobile_phone_2){
        ContentValues values = new ContentValues();
        values.put(KEY_TEMPLATE_NAME, template_name);
        values.put(KEY_NUMBER_OF_CONTACTS, number_of_contacts);
        values.put(KEY_FIRST_NAME, first_name);
        values.put(KEY_LAST_NAME, last_name);
        values.put(KEY_MIDDLE_NAME, middle_name);
        values.put(KEY_JOB_TITLE, job_title);
        values.put(KEY_HOME_PHONE_1, home_phone_1);
        values.put(KEY_HOME_PHONE_2, home_phone_2);
        values.put(KEY_MOBILE_PHONE_1, mobile_phone_1);
        values.put(KEY_MOBILE_PHONE_2, mobile_phone_2);
        return db.update(DATABASE_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
    }
}