I would like to implement a database which consists of 2 tables, number and receivernumber. In fact, I have 2 xml files, one using number table, another using receivernumber. However, the code below only allows the first table to be used for both of the xmls. 

Previously, I have tried implementing 2 different sets of class files, as stated below. But it crashes the entire app instead. So I tried changing to using the same class file with the receivernumber table being number2 instead. Now, both of the activity/xml file uses the first database but not the second. Please advice. Thanks in advance. 

Note: there are no error logs available as it is working. Just that the table used for both activities are the same, but it's supposed to be different.

Set\_numbers\_to\_forward.java

```
import java.util.ArrayList;
import java.util.List;

import com.example.awkwardpanda_redirectcall.DBAdapter;
import com.example.awkwardpanda_redirectcall.CallAdapter;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;

public class Set_numbers_to_forward extends Activity {

Context mContext;
ArrayAdapter<String> adapter;
List<String> adapterData;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set_numbers_to_forward);
    mContext = this;
    ListView list = (ListView) findViewById(R.id.number_list);

    adapterData = new ArrayList<String>();
    DBAdapter db = new DBAdapter(this);

    // ---get all contacts---
    db.open();
    Cursor c = db.getAllContacts();
    if (c.moveToFirst()) {
        do {
            adapterData.add(c.getString(1));
        } while (c.moveToNext());
    }
    db.close();

    adapter = new CallAdapter(this,
            R.layout.set_numbers_to_forward_editable, adapterData);
    list.setAdapter(adapter);   

} // end onCreate

public void onCreate2(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.set_number_to_receive);
    mContext = this;
    ListView list = (ListView) findViewById(R.id.receiver_number);

    adapterData = new ArrayList<String>();
    DBAdapter db = new DBAdapter(this);

    // ---get all contacts---
    db.open();
    Cursor c = db.getAllContacts();
    if (c.moveToFirst()) {
        do {
            adapterData.add(c.getString(1));
        } while (c.moveToNext());
    }
    db.close();

    adapter = new CallAdapter(this,
            R.layout.set_number_to_receive_editable, adapterData);
    list.setAdapter(adapter);

} // end onCreate

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("New Phone Number");
    builder.setMessage("Enter Phone Number :");
    final EditText input = new EditText(Set_numbers_to_forward.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    builder.setView(input);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            DBAdapter db = new DBAdapter(mContext);
            db.open();
            db.insertContact(input.getText().toString());
            db.close();
            adapterData.add(input.getText().toString());
            adapter.notifyDataSetChanged();
        }
    });
    builder.create().show();
    return true;
}

public boolean onOptionsItemSelected2(MenuItem item) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("New Phone Number");
    builder.setMessage("Enter Phone Number :");
    final EditText input = new EditText(Set_numbers_to_forward.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    builder.setView(input);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            DBAdapter db = new DBAdapter(mContext);
            db.open();
            db.insertContact2(input.getText().toString());
            db.close();
            adapterData.add(input.getText().toString());
            adapter.notifyDataSetChanged();
        }
    });
    builder.create().show();
    return true;
}

}

```

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 = "hpnumberID";
public static final String KEY_NAME = "hpNumber";
private static final String TAG = "DBAdapter";

public static final String KEY_ROWID2 = "hpnumberID";
public static final String KEY_NAME2 = "hpNumber";

private static final String DATABASE_NAME = "HPnumberDB";
private static final String DATABASE_TABLE = "number";
private static final String DATABASE_TABLE2 = "receivernumber";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE =
    "create table number (hpnumberID integer primary key autoincrement, "
    + "hpNumber text not null);";

private static final String DATABASE_CREATE2 =
        "create table receivernumber (hpnumberID integer primary key autoincrement, "
        + "hpNumber 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);

    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try {
            db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    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 " + DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
        onCreate(db);
    }

} // end DatabaseHelper class    

  //---opens the database---
public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a contact into the database---
public long insertContact(String hpNumber) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, hpNumber);
    return db.insert(DATABASE_TABLE, null, initialValues);

}

public long insertContact2(String hpNumber) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME2, hpNumber);
    return db.insert(DATABASE_TABLE2, null, initialValues);

}

//---deletes a particular contact---
public boolean deleteContact(long hpnumberID) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + hpnumberID, null) > 0;
}

public boolean deleteContact2(long hpnumberID) 
{
    return db.delete(DATABASE_TABLE2, KEY_ROWID2 + "=" + hpnumberID, null) > 0;
}

//---retrieves all the contacts---
public Cursor getAllContacts() 
{
    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, null, null,         
null, null, null);
}

public Cursor getAllContacts2() 
{
    return db.query(DATABASE_TABLE2, new String[] {KEY_ROWID2, KEY_NAME2}, null, null, 
null, null, null);
}

//---retrieves a particular contact---
public Cursor getContact(long hpnumberID) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
            KEY_NAME}, KEY_ROWID + "=" + hpnumberID, null,
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public Cursor getContact2(long hpnumberID) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE2, new String[] {KEY_ROWID2,
            KEY_NAME2}, KEY_ROWID2 + "=" + hpnumberID, null,
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a contact---
public boolean updateContact(long hpnumberID, String hpNumber) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_NAME, hpNumber);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + hpnumberID, null) > 0;
}

public boolean updateContact2(long hpnumberID, String hpNumber) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_NAME2, hpNumber);
    return db.update(DATABASE_TABLE2, args, KEY_ROWID2 + "=" + hpnumberID, null) > 0;
}

} // end DBAdapter class

```

CallAdapter.java

```
import java.util.List;

import android.app.AlertDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CallAdapter extends ArrayAdapter<String> {

Context mContext;
List<String> list;

public CallAdapter(Context context, int resource, List<String> list) {
    super(context, resource, list);
    // TODO Auto-generated constructor stub
    mContext = context;
    this.list = list;
}

@Override 
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater
            .inflate(R.layout.set_numbers_to_forward_editable, parent, 
false);
    TextView hpNumber = (TextView) rowView.findViewById(R.id.hp_Number);
    ImageView discardButton = (ImageView) rowView
            .findViewById(R.id.number_discard_button);
    ImageView editButton = (ImageView) rowView
            .findViewById(R.id.number_edit_button);
    hpNumber.setText(list.get(position));
    discardButton.setTag(position);
    editButton.setTag(position);
    editButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String position = v.getTag().toString();
            createDialog(position, list);
        }

    });
    discardButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            createDeleteDialog(v.getTag().toString(), list);

        }

    });
    return rowView;
}

public View getView2(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater
            .inflate(R.layout.set_number_to_receive_editable, parent, 
false);
    TextView hpNumber = (TextView) rowView.findViewById(R.id.hp_Number);
    ImageView discardButton = (ImageView) rowView
            .findViewById(R.id.number_discard_button);
    ImageView editButton = (ImageView) rowView
            .findViewById(R.id.number_edit_button);
    hpNumber.setText(list.get(position));
    discardButton.setTag(position);
    editButton.setTag(position);
    editButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String position = v.getTag().toString();
            createDialog(position, list);
        }

    });
    discardButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            createDeleteDialog(v.getTag().toString(), list);

        }

    });
    return rowView;
}

public void createDialog(String position, List<String> list) {

    final long hpnumberID = Long.parseLong(position) + 1;
    final int viewPosition = Integer.parseInt(position);
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle("Edit");
    final EditText input = new EditText(mContext);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    input.setText(list.get(viewPosition));
    builder.setView(input);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            DBAdapter db = new DBAdapter(mContext);
            db.open();
            db.updateContact(hpnumberID, input.getText().toString());
            db.close();
            ((Set_numbers_to_forward) 
mContext).adapterData.remove(viewPosition);
            ((Set_numbers_to_forward) 
mContext).adapterData.add(viewPosition, input
                    .getText().toString());
            ((Set_numbers_to_forward) 
mContext).adapter.notifyDataSetChanged();
        }
    });

    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int 
which) {
                    dialog.dismiss();
                }
            });
    builder.create().show();
}

public void createDeleteDialog(String position, List<String> list) {

    final int viewPosition = Integer.parseInt(position);
    final long hpnumberID = Long.parseLong(position) + 1;
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setMessage("Deleting \"" + list.get(viewPosition) + "\"");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            DBAdapter db = new DBAdapter(mContext);
            db.open();
            db.deleteContact(hpnumberID);
            db.close();
            ((Set_numbers_to_forward) 
mContext).adapterData.remove(viewPosition);
            ((Set_numbers_to_forward) 
mContext).adapter.notifyDataSetChanged();
        }
    });
    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int 
which) {
                    dialog.dismiss();
                }
            });

    builder.create().show();
}

```

}