Android数据库EditText NumberSigned

时间:2015-02-05 13:03:11

标签: java android database android-sqlite

我用两个edittext创建了一个android SQLDatabase。一个保存名称,另一个保存一些数字。这是我的活动

 public class AccountActivity extends ActionBarActivity {

        int from_Where_I_Am_Coming = 0;
        private DBHelper mydb ;
        TextView name ;
        TextView amount;
        int id_To_Update = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account);

    name = (TextView) findViewById(R.id.input_name);
    amount = (TextView) findViewById(R.id.input_amount);

    mydb = new DBHelper(this);

    Bundle extras = getIntent().getExtras();
    if(extras !=null) {
        int Value = extras.getInt("id");
        if (Value > 0) {
            //means this is the view part not the add contact part.
            Cursor rs = mydb.getData(Value);
            id_To_Update = Value;
            rs.moveToFirst();
            String nam = rs.getString(rs.getColumnIndex(DBHelper.ACCOUNT_COLUMN_NAME));
            String amo = rs.getString(rs.getColumnIndex(DBHelper.ACCOUNT_COLUMN_AMOUNT));
            if (!rs.isClosed()) {
                rs.close();
            }
            Button b = (Button) findViewById(R.id.btn_save);
            b.setVisibility(View.INVISIBLE);

            name.setText((CharSequence) nam);
            name.setFocusable(false);
            name.setClickable(false);

            amount.setText((CharSequence) amo);
            amount.setFocusable(false);
            amount.setClickable(false);
        }
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    Bundle extras = getIntent().getExtras();
    if(extras !=null)
    {
        int Value = extras.getInt("id");
        if(Value>0){
            getMenuInflater().inflate(R.menu.menu_account, menu);
        }
        else{
            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;
        }

        switch(item.getItemId())
        {
            case R.id.Edit_Contact:
                Button b = (Button)findViewById(R.id.btn_save);
                b.setVisibility(View.VISIBLE);
                name.setEnabled(true);
                name.setFocusableInTouchMode(true);
                name.setClickable(true);

                amount.setEnabled(true);
                amount.setFocusableInTouchMode(true);
                amount.setClickable(true);

                return true;
            case R.id.Delete_Contact:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.deleteAccount)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mydb.deleteAccount(id_To_Update);
                                Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(getApplicationContext(),com.tubapps.accountdb.MainActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                            }
                        });
                AlertDialog d = builder.create();
                d.setTitle("Are you sure");
                d.show();

                return true;

            default:
        return super.onOptionsItemSelected(item);
    }
}
    public void run(View view)
    {
        Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
            int Value = extras.getInt("id");
            if(Value>0){
                if(mydb.updateAccount(id_To_Update, name.getText().toString(), amount.getText().length())){
                    Intent intent = new Intent(getApplicationContext(),com.tubapps.accountdb.MainActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                if(mydb.insertAccount(name.getText().toString(), amount.getText().length())){
                }
                else{
                }
                Intent intent = new Intent(getApplicationContext(),com.tubapps.accountdb.MainActivity.class);
                startActivity(intent);
            }
        }
    }
}

这是我的xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0EEEE"
    tools:context="com.tubapps.accountdb.AccountActivity">

            <EditText
                android:id="@+id/input_name"
                android:layout_width="fill_parent"
                android:inputType="text"
                android:layout_height="wrap_content"
                android:hint="@string/account_name"
                android:layout_marginLeft="8dip"
                android:layout_marginRight="8dip" />

            <EditText
                android:id="@+id/input_amount"
                android:layout_width="fill_parent"
                android:inputType="numberSigned"
                android:layout_height="wrap_content"
                android:hint="@string/initial_value"
                android:layout_below="@+id/input_name"
                android:layout_marginLeft="8dip"
                android:layout_marginRight="8dip" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:orientation="horizontal"
        android:showDividers="middle">

        <Button
            android:id="@+id/btn_cnc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:clickable="true"
            android:focusable="true"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/btn_save"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="run"
            android:background="#FFFFFF"
            android:text="@string/save_income" />
    </LinearLayout>

</RelativeLayout>

这是我的DBHelper。

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDBName.db";
    public static final String ACCOUNT_TABLE_NAME = "accounts";
    public static final String ACCOUNT_COLUMN_ID = "id";
    public static final String ACCOUNT_COLUMN_NAME = "name";
    public static final String ACCOUNT_COLUMN_AMOUNT = "amount";

    private HashMap hp;

    public DBHelper(Context context)
    {
        super(context, DATABASE_NAME , null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(
                "create table accounts " +
                        "(id integer primary key, name text,amount integer)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS accounts");
        onCreate(db);
    }

    public boolean insertAccount  (String name, Integer amount)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("name", name);
        contentValues.put("amount", amount);

        db.insert("accounts", null, contentValues);
        return true;
    }
    public Cursor getData(int id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from accounts where id="+id+"", null );
        return res;
    }
    public int numberOfRows(){
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, ACCOUNT_TABLE_NAME);
        return numRows;
    }
    public boolean updateAccount (Integer id, String name, Integer amount)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("amount", amount);
        db.update("accounts", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
        return true;
    }

    public Integer deleteAccount (Integer id)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("accounts",
                "id = ? ",
                new String[] { Integer.toString(id) });
    }
    public ArrayList getAllAccounts()
    {
        ArrayList array_list = new ArrayList();
        //hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from accounts", null );
        res.moveToFirst();
        while(res.isAfterLast() == false){
            array_list.add(res.getString(res.getColumnIndex(ACCOUNT_COLUMN_NAME)));
            res.moveToNext();
        }
        return array_list;
    }
}

当我插入一个数字时,它只保存数字3.我不知道问题出在哪里。

1 个答案:

答案 0 :(得分:1)

你正在获得&#34;长度&#34;数字,而不是这些行的价值:

amount.getText().length()

我怀疑你应该做的事情如下:

Integer.valueOf(amount.getText())