我的应用有问题。这就是它,它确实不是一个错误,但我认为这个需要修复,以获得我的应用程序的最佳体验。 在我的应用程序中,有一个日期选择器,用户用它来选择一些日期(dd-mm-yyyy),应用程序将它们保存到数据库中。 它运行良好,我可以插入并选择数据库中的日期。但我混淆的是为什么日期格式化为一个奇怪的词?我已经阅读了一些关于日期时间的书籍,但我仍然无法找到如何使我的日期格式化为(dd-mm-yyyy)并在列表视图中显示它。我使用Calender.get(日历)来调用datepicker。
数据库上的日期列现在只包含值“2039”或“2040”等,而不是17-08-2015。
我不知道该怎么办,请高手帮我解决这个问题。 谢谢。
的Nb。这是我的NewTransaction代码(插入活动):
public class NewTransaction extends ActionBarActivity {
Button btnIDate;
Button btnIAdd;
Button btnICancel;
RadioButton RdIncome;
RadioButton RdOutcome;
EditText txtAmount, txtCashflow, txtType;
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db;
MainActivity mainAct = new MainActivity();
int year_x, month_x, day_x;
static final int DIALOG_ID=0;
public static long dateSelected;
public static Integer intAmount = null;
public static String month;
private boolean isUpdateTrans;
private String id, transname, transamount, transtype, transdate, transcategid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_transaction);
txtAmount = (EditText)findViewById(R.id.txtAmount);
txtCashflow = (EditText)findViewById(R.id.txtCashflow);
txtType = (EditText)findViewById(R.id.txtType);
RdIncome = (RadioButton)findViewById(R.id.RdBtnIncome);
RdOutcome = (RadioButton)findViewById(R.id.RdBtnOutcome);
String catSelected = mainAct.getCatSelected();
onButtonClickButtonListener(catSelected);
selectDate();
showDialogOnClick();
//kena null object dsni entah knapa
Bundle bundle= getIntent().getExtras();
if(bundle!= null) {
isUpdateTrans = getIntent().getExtras().getBoolean("update");
if (isUpdateTrans) {
System.out.print("isUpdate di NewTrans");
id = getIntent().getExtras().getString("TransId");
transname = getIntent().getExtras().getString("TransName");
transamount = getIntent().getExtras().getString("TransAmount");
transtype = getIntent().getExtras().getString("TransType");
transdate = getIntent().getExtras().getString("CategDate");
transcategid = getIntent().getExtras().getString("CategCategId");
txtCashflow.setText(transname);
txtType.setText(transtype);
txtAmount.setText(transamount);
btnIDate.setText(transdate);
}
}
}
private void selectDate(){
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
switchMonth(month_x);
btnIDate = (Button)findViewById(R.id.btnDate);
btnIDate.setText("Date : " + day_x + "-" + month + "-" + year_x);
//dateSelected = (year_x+month_x+day_x);
}
public void showDialogOnClick(){
btnIDate = (Button)findViewById(R.id.btnDate);
btnIDate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog(int id){
if (id == DIALOG_ID)
return new DatePickerDialog(this, dpickerListener , year_x, month_x, day_x);
return null;
}
private String switchMonth(Integer month_x){
switch(month_x){
case 1 : month = "Jan";
break;
case 2 : month = "Feb";
break;
case 3 : month = "Mar";
break;
case 4 : month = "Apr";
break;
case 5 : month = "Mei";
break;
case 6 : month = "Jun";
break;
case 7 : month = "Jul";
break;
case 8 : month = "Aug";
break;
case 9 : month = "Sep";
break;
case 10 : month = "Okt";
break;
case 11 : month = "Nov";
break;
case 12 : month = "Des";
break;
}
return month;
}
public DatePickerDialog.OnDateSetListener dpickerListener
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x= year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
switchMonth(month_x);
btnIDate = (Button)findViewById(R.id.btnDate);
btnIDate.setText("Date : " + day_x + "-" + month + "-" + year_x);
Toast.makeText(NewTransaction.this, year_x + "/" + month_x + "/" + day_x, Toast.LENGTH_LONG).show();
//add new code
dateSelected = view.getCalendarView().getDate();
//DateFormat.getDateInstance().format(myDatePicker.getCalendarView().getDate());
}
};
private void clearText(){
txtCashflow.clearComposingText();
txtAmount.clearComposingText();
txtType.clearComposingText();
}
public void onButtonClickButtonListener(final String catSelected){
btnIAdd = (Button)findViewById(R.id.btnAddTrans);
btnIAdd.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(RdIncome.isChecked()){
txtType.setText("Income");
}else{
txtType.setText("Outcome");
}
if (isUpdateTrans) {
//update
Toast.makeText(NewTransaction.this, "Clicked", Toast.LENGTH_LONG).show();
intAmount = Integer.parseInt(txtAmount.getText().toString());
boolean isInserted = dbHelper.updateTransData(id, txtCashflow.getText().toString(),
intAmount, txtType.getText().toString(), dateSelected, catSelected, null);
if (isInserted == true) {
Toast.makeText(NewTransaction.this, "Updated", Toast.LENGTH_LONG).show();
clearText();
Intent intent = new Intent(
NewTransaction.this,
ViewCashflow.class
);
startActivity(intent);
} else
Toast.makeText(NewTransaction.this, "Not Inserted", Toast.LENGTH_LONG).show();
} else {
//insert
Toast.makeText(NewTransaction.this, "Clicked", Toast.LENGTH_LONG).show();
intAmount = Integer.parseInt(txtAmount.getText().toString());
boolean isInserted = dbHelper.insertTransData(txtCashflow.getText().toString(),
intAmount, txtType.getText().toString(), dateSelected, catSelected, null);
if (isInserted == true) {
Toast.makeText(NewTransaction.this, "Inserted", Toast.LENGTH_LONG).show();
clearText();
Intent intent = new Intent(
NewTransaction.this,
ViewCashflow.class
);
startActivity(intent);
} else
Toast.makeText(NewTransaction.this, "Not Inserted", Toast.LENGTH_LONG).show();
}
}
});
btnICancel = (Button)findViewById(R.id.btnCancelTrans);
btnICancel.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
NewTransaction.this,
MainActivity.class
);
startActivity(intent);
}
}
);
}
@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_new_transaction_, 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);
}
}
这个用于ViewCashFlow(从数据库中选择数据包括日期并将它们填充到listView中的活动):
public class ViewCashflow extends ActionBarActivity {
Button btnIDate1, btnIDate2, btnIAddNewTrans;
private SQLiteDatabase db;
private static Button BtnIAddCateg;
private static Button BtnICancelCateg;
private static final String TAG = CategorySetting.class.getSimpleName();
DatabaseHelper dBHelper = new DatabaseHelper (this);
private ListView list;
int year_x, month_x, day_x;
static final int DIALOG_ID=0;
static final int DIALOG_ID2=1;
public static String month ;
public static long dateSelected1, dateSelected2;
private ArrayList<String> arrTransId = new ArrayList<String>();
private ArrayList<String> arrTransName = new ArrayList<String>();
private ArrayList<String> arrTransAmount = new ArrayList<String>();
private ArrayList<String> arrTransType= new ArrayList<String>();
private ArrayList<String> arrTransDate= new ArrayList<String>();
private ArrayList<String> arrCategId= new ArrayList<String>();
private AlertDialog.Builder build;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_cashflow);
btnIDate1 = (Button)findViewById(R.id.btnDate1);
btnIDate2 = (Button)findViewById(R.id.btnDate2);
onButtonClickButtonListener();
//calendernya lari lari
showDialogOnClick();
selectDate();
displayData();
}
private void displayData() {
db = dBHelper.getReadableDatabase();
Cursor mCursor = db.rawQuery("SELECT * FROM " + dBHelper.TABLE_Trans_NAME + " WHERE TransDate Between "
+ dateSelected1+" AND " + dateSelected2, null);
list = (ListView)findViewById(android.R.id.list);
arrTransId.clear();
arrTransName.clear();
arrTransAmount.clear();
arrTransType.clear();
arrTransDate.clear();
arrCategId.clear();
if (mCursor.moveToFirst()) {
do {
arrTransId.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL1)));
arrTransName.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL2)));
arrTransAmount.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL3)));
arrTransType.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL4)));
arrTransDate.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL5)));
arrCategId.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL6)));
} while (mCursor.moveToNext());
}
DisplayAdapterTrans disadptr = new DisplayAdapterTrans(ViewCashflow.this, arrTransId, arrTransName,
arrTransAmount, arrTransType, arrTransDate, arrCategId);
list.setAdapter(disadptr);
mCursor.close();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent i = new Intent(getApplicationContext(), NewTransaction.class);
i.putExtra("TransId", arrTransId.get(arg2));
i.putExtra("TransName", arrTransName.get(arg2));
i.putExtra("TransAmount", arrTransAmount.get(arg2));
i.putExtra("TransType", arrTransType.get(arg2));
i.putExtra("TransDate", arrTransDate.get(arg2));
i.putExtra("TransCategId", arrCategId.get(arg2));
i.putExtra("update", true);
startActivity(i);
}
});
}
public void onButtonClickButtonListener(){
btnIAddNewTrans = (Button)findViewById(R.id.btnNewTransonView);
btnIAddNewTrans.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
ViewCashflow.this,
NewTransaction.class
);
startActivity(intent);
}
}
);
}
private void selectDate(){
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
switchMonth(month_x);
btnIDate1.setText("Date : " + day_x + "-" + month + "-" + year_x);
btnIDate2.setText("Date : " + day_x + "-" + month + "-" + year_x);
dateSelected1 = (year_x+month_x+day_x);
dateSelected2 = (year_x+month_x+day_x);
}
public void showDialogOnClick(){
btnIDate1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
displayData();
}
}
);
btnIDate2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID2);
displayData();
}
}
);
}
@Override
protected Dialog onCreateDialog(int id){
if (id == DIALOG_ID) {
return new DatePickerDialog(this, dpickerListener, year_x, month_x, day_x);
}
else if(id == DIALOG_ID2) {
return new DatePickerDialog(this, dpickerListener2, year_x, month_x, day_x);
}
return null;
}
private String switchMonth(Integer month_x){
switch(month_x){
case 1 : month = "Jan";
break;
case 2 : month = "Feb";
break;
case 3 : month = "Mar";
break;
case 4 : month = "Apr";
break;
case 5 : month = "Mei";
break;
case 6 : month = "Jun";
break;
case 7 : month = "Jul";
break;
case 8 : month = "Aug";
break;
case 9 : month = "Sep";
break;
case 10 : month = "Okt";
break;
case 11 : month = "Nov";
break;
case 12 : month = "Des";
break;
}
return month;
}
public DatePickerDialog.OnDateSetListener dpickerListener
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x= year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
switchMonth(month_x);
btnIDate1.setText("Date : " + day_x + "-" + month + "-" + year_x);
dateSelected1 = (year_x+month_x+day_x);
//Toast.makeText(ViewCashflow.this, year_x + "/" + month_x + "/" + day_x, Toast.LENGTH_LONG).show();
Toast.makeText(ViewCashflow.this, "date " + dateSelected1, Toast.LENGTH_LONG).show();
//DateFormat.getDateInstance().format(myDatePicker.getCalendarView().getDate());
System.out.print("date 1: " + dateSelected1);
}
};
public DatePickerDialog.OnDateSetListener dpickerListener2
= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x= year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
switchMonth(month_x);
btnIDate2.setText("Date : " + day_x + "-" + month + "-" + year_x);
dateSelected2 = (year_x+month_x+day_x);
//Toast.makeText(ViewCashflow.this, year_x + "/" + month_x + "/" + day_x, Toast.LENGTH_LONG).show();
Toast.makeText(ViewCashflow.this, "date " + dateSelected2, Toast.LENGTH_LONG).show();
//DateFormat.getDateInstance().format(myDatePicker.getCalendarView().getDate());
System.out.print("date 2"+ dateSelected2);
}
};
@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_view_cashflow, 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);
}
}
答案 0 :(得分:0)
您无法使用dateSelected1 = (year_x+month_x+day_x);
它将这三个整数相加。
17 + 8 + 2015 = 2040。
尝试使用dateSelected1 = view.getCalendarView().getDate();
onDateSet
方法{/ 1}}上的DatePickerDialog.OnDateSetListener
。