Im able to write data to a the database in the main activity but im having problems reading and displaying the data in a different activity. I'm using a SQLHelperDatabase class with a 'addData' method (which works fine according to the debugger) and a showData method (and this is where the problem lies). After entering data in the main activity i want to be able to press the send message button and the data to be displayed on the new activity. Ive been stuck on this for a week now and i been searching the WWW and every tutorial says that my code should work. Please can someone help me get to the bottom of this frustrating ordeal.
package com.doors.waynderful.myapp;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHelper(this);
Cursor cursor = db.showData(1);
cursor.moveToFirst();
String nam = cursor.getString(cursor.getColumnIndex(DatabaseHelper.THE_TABLE_COLUMN_NAME));
String det = cursor.getString(cursor.getColumnIndex(DatabaseHelper.THE_TABLE_COLUMN_NAME));
if (!cursor.isClosed()){
cursor.close();
}
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(nam);
setContentView(textView);
//setContentView(R.layout.activity_display_message);
}
@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);
}
}
Below is the SQLHelperDatabase.
package com.doors.waynderful.myapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
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(int idIn)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
cursor = db.rawQuery("select * from "+THE_TABLE+" where id = "+1+"", null);
return cursor;
}
}
Below is the MainActivity
package com.doors.waynderful.myapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
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);
//intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
Aucun commentaire:
Enregistrer un commentaire