I made an SQLite Database and I wanna know how to save the date.
This is my activity.
public class IncomeActivity extends ActionBarActivity implements View.OnClickListener {
int from_Where_I_Am_Coming = 0;
private DBHelper mydb ;
TextView payer ;
TextView amount;
TextView date;
int id_To_Update = 0;
DateFormat formate = DateFormat.getDateInstance();
Calendar calendar = Calendar.getInstance();
Button label;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income);
payer = (TextView) findViewById(R.id.input_payer);
amount = (TextView) findViewById(R.id.input_amount);
date = (Button) findViewById(R.id.input_date);
label = (Button) findViewById(R.id.input_date);
btn = (Button) findViewById(R.id.input_date);
btn.setOnClickListener(this);
updatedate();
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 amo = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_AMOUNT));
String pyr = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_PAYER));
if (!rs.isClosed()) {
rs.close();
}
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.INVISIBLE);
Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.INVISIBLE);
amount.setText((CharSequence) amo);
amount.setFocusable(false);
amount.setClickable(false);
payer.setText((CharSequence) pyr);
payer.setFocusable(false);
payer.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_income, 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_Income:
Button save = (Button)findViewById(R.id.btn_save);
save.setVisibility(View.VISIBLE);
Button cancel = (Button)findViewById(R.id.btn_cnc);
cancel.setVisibility(View.VISIBLE);
amount.setEnabled(true);
amount.setFocusableInTouchMode(true);
amount.setClickable(true);
payer.setEnabled(true);
payer.setFocusableInTouchMode(true);
payer.setClickable(true);
return true;
case R.id.Delete_Income:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteIncome)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteIncome(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.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.updateIncome(id_To_Update, amount.getText().toString(), payer.getText().toString(), date.getText().toString())){
Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
}
else{
if(mydb.insertIncome(amount.getText().toString(), payer.getText().toString(), date.getText().toString())){
}
else{
}
Intent intent = new Intent(getApplicationContext(),tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
}
}
@Override
public void onClick(View v) {
SetDate();
}
public void updatedate() {
label.setText(formate.format(calendar.getTime()));
}
public void SetDate() {
new DatePickerDialog(IncomeActivity.this, d, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)).show();
ContentValues values = new ContentValues();
}
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updatedate();
}
};
}
This is my DBHelper.
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 incomes " +
"(id integer primary key, amount string,payer string, date string)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS incomes");
onCreate(db);
}
public boolean insertIncome (String amount, String payer, String date)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payer", payer);
contentValues.put("date", date);
db.insert("incomes", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from incomes where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, INCOME_TABLE_NAME);
return numRows;
}
public boolean updateIncome (Integer id, String amount, String payer, String date)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payer", payer);
contentValues.put("date", date);
db.update("incomes", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteIncome (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("incomes",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList getAllIncomes()
{
ArrayList array_list = new ArrayList();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from incomes", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(INCOME_COLUMN_PAYER)));
res.moveToNext();
}
return array_list;
}
}
And this is my XML.
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tubapps.datepickerdb.IncomeActivity">
<android.support.v7.widget.CardView
android:id="@+id/inc_card_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#FFFFFF">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/input_amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:hint="@string/income_amount"
android:inputType="numberSigned|numberDecimal" />
<EditText
android:id="@+id/input_payer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/input_amount"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:hint="@string/income_payer"
android:inputType="text" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/date_btn"
android:background="#FFFFFF"
android:id="@+id/input_date"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<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:onClick="run"
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:background="#FFFFFF"
android:onClick="run"
android:text="@string/save_income" />
</LinearLayout>
</RelativeLayout>
The date actually is a button and when I save the database it just save the textviews and not the date to. And if I introduce this String date= rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_DATE)); it gives me an error.
Aucun commentaire:
Enregistrer un commentaire