0

したがって、私が知っているテキストビューはデータベースからロードされていますが、希望どおりではありません...

私はできる限りこれを説明しようとします:

データベース:

public class DBAdapter_Name {
public static final String KEY_ROWID = "_id";
public static final String KEY_ITEM = "name";

private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "NameDB";
private static final String DATABASE_TABLE = "charactername";
private static final int DATABASE_VERSION = 5;

private static final String DATABASE_CREATE = "create table if not exists charactername (_id integer primary key autoincrement, "
        + "name VARCHAR not null);";

private final Context context;

private DatabaseHelper DBHelper_name;
private static SQLiteDatabase dbname;

public DBAdapter_Name(Context ctx) {
    this.context = ctx;
    DBHelper_name = new DatabaseHelper(context);
}

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

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

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

// ---opens the database---
public DBAdapter_Name open() throws SQLException { 
    dbname = DBHelper_name.getWritableDatabase();
    return this;
}

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

// ---insert a record into the database---
public long insertRecord(String name) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_ITEM, name);
    return dbname.insert(DATABASE_TABLE, null, initialValues);
}

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

// ---retrieves all the records---
public static Cursor getAllRecords() {
    return dbname.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM},
            null, null, null, null, null, null);
}

// ---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException {
    Cursor mCursor = dbname.query(true, DATABASE_TABLE, new String[] {
            KEY_ROWID, KEY_ITEM }, KEY_ROWID + "=" + rowId, null, null,
            null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public int deleteAll(){
    return 0;

     }

// ---updates a record---
public boolean updateRecord(long rowId, String name) {
    ContentValues args = new ContentValues();
    args.put(KEY_ITEM, name);

    return dbname.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}

レコードの挿入方法:

public class CreateNew extends Activity {

DBAdapter_Name dbname = new DBAdapter_Name(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_new)
                Button btnCreate = (Button) findViewById(R.id.btnCreate);
    btnCreate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText editText1 = (EditText) findViewById(R.id.editText1);
            String nameTxt = editText1.getText().toString();
Intent PlayScreen = new Intent(getApplicationContext(),
                        PlayScreen.class);
                // ------------------------------------------------------------------------------------------------------------------------------------
                Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
                Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
                Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
                // ------------------------------------------------------------------------------------------------------------------------------------
                dbname.open();
                long insertedRowId = dbname.insertRecord(editText1.getText().toString()); //Get the rowId which was inserted.
                dbname.close();
                PlayScreen.putExtra("strAlliance", spinner1
                        .getSelectedItem().toString());
                PlayScreen.putExtra("strRace", spinner2.getSelectedItem()
                        .toString());
                PlayScreen.putExtra("strClass", spinner3.getSelectedItem()
                        .toString());
                // ------------------------------------------------------------------------------------------------------------------------------------
                startActivityForResult(PlayScreen, 0);
            }
        }
    });

    Button btnMoreInfo = (Button) findViewById(R.id.btnMoreInfo);
    btnMoreInfo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent MoreInfo = new Intent(v.getContext(), MoreInfo.class);
            startActivity(MoreInfo);

        }
    });
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent BeforeYouBegin = new Intent(getApplicationContext(),
            BeforeYouBegin.class);
    startActivity(BeforeYouBegin);
    finish();
}

editText は明らかに、名前を入力する場所です

そして、どのようにレコードを表示していますか:

public void DisplayRecord(Cursor c) {
    TextView textViewStrName = (TextView) findViewById(R.id.textViewStrName);
    Cursor result = dbname.getRecord(insertedRowId);
    int columnIndex = result.getColumnIndex(DBAdapter_Name.KEY_ITEM);
    String insertedName = result.getString(columnIndex);
    textViewStrName.setText(insertedName);
}

これで、入力した内容を表示するときに editText ボックスに何を入力しても、表示されるのは名前だけです。

だから、データベースを読んでいますが、私が望む方法ではありません....

4

2 に答える 2

0

DBに入力した内容を表示するには、これが必要です。

//Inserting the value into DB.
dbname.open();
long insertedRowId = dbname.insertRecord(editText1.getText().toString()); //Get the rowId which was inserted.
dbname.close();

//Retrieving that value from DB & displaying it.
Cursor result = dbname.getRecord(insertedRowId);;
int columnIndex = result.getColumnIndex(DBAdapter_Name.KEY_ITEM);
String insertedName = result.getString(columnIndex);
textViewStrName.setText(insertedName);
于 2013-03-18T03:47:30.460 に答える
0

このコードを試してください

  String name="";
dbname.open();
long rowId = dbname.insertRecord(editText1.getText().toString());
Cursor c = dbname.getRecord(rowId);
    startManagingCursor(c);
    if(c.moveToFirst()){
        do{
             name = c.getString(1);
        }while (c.moveToNext());
    }
    dbname.close();
    c.close();

    textViewStrName.setText(name);

これがお役に立てば幸いです。

于 2013-03-18T04:17:14.830 に答える