lundi 27 avril 2015

Android SQLite database

Im trying to read from the database display data on another Activity. Where am i going wrong? the program crashes when the second activity attempts to open. The Debugger indicates to the cursor is the problem, but i just dont know what the issue is.

This is the databasehelper

public class DatabaseHelper extends SQLiteOpenHelper
{
public static final String THE_DATABASE = "theDatabase";
public static final String THE_TABLE = "infoTable";
public static final String THE_TABLE_COLUMN_ID = "id";
public static final String THE_TABLE_COLUMN_NAME = "name";
public static final String THE_TABLE_COLUMN_DETAILS = "details";

DatabaseHelper(Context context)
{
    super(context,THE_DATABASE, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " +THE_TABLE+"(id integer primary key, name     text, details text) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists "+ THE_TABLE+ " ");

}

public void addData(String nameIn, String detailsIn)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues data = new ContentValues();

    data.put("name", nameIn);
    data.put("details", detailsIn);

    db.insert(THE_TABLE,null,data);

}

public Cursor showData()
{
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor;
    cursor = db.rawQuery("select * from "+THE_TABLE+"", null);
    return cursor;

}
}

This is the mainActivity

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE =     "com.doors.waynderful.myapp.MESSAGE";
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view){
    myDb = new DatabaseHelper(this);


    Intent intent = new Intent(this,DisplayMessageActivity.class);

    EditText editText = (EditText) findViewById(R.id.edit_name);
    EditText editText1 = (EditText) findViewById(R.id.edit_details);

    String name = editText.getText().toString();
    String details = editText1.getText().toString();

    myDb.addData(name, details);

   startActivity(intent);

}
}

This is the Activity to display the data from the database

public class DisplayMessageActivity extends ActionBarActivity {
TextView Tview;
DatabaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        Tview = (TextView) findViewById(R.id.box);

        db = new DatabaseHelper(this);
        Cursor cursor = db.showData();
        cursor.moveToFirst();

        String nam = cursor.getString(cursor.getColumnIndex(db.THE_TABLE_COLUMN_NAME));

        if (!cursor.isClosed()){
            cursor.close();
        }

    Tview.setText(nam);

}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Aucun commentaire:

Enregistrer un commentaire