我编写了一个程序来插入一些数据,其中还包含一个名为date的列,因此当我插入内容时,日期也会同时插入。
插入工作完美,现在处于不同的活动中我有两个来自日期和时间的编辑单位,当点击单独的日期选择器将打开并且所选日期将在相应的编辑文本上设置,现在我想要做的就是当我完成从日期和日期中选择,我希望我的微调器根据我插入数据的日期进行填充。
我尝试了一些不起作用的东西。
这是我创建的表格
private static final String CREATE_TABLE_FINALDATA = "Create table
"+TABLE_NAME_FINALDATA+" (" +UID
+" INTEGER PRIMARY KEY, "+REFERENCE_NO+" VARCHAR(200),
"+ACCOUNT_NAME+" VARCHAR(200), "+ACCOUNT_TYPE+" VARCHAR(100)
,"+VEHICLE_NO+" VARCHAR(100), "+DRIVER_CODE+" VARCHAR(50),
"+DRIVER_NAME+" VARCHAR(200), "+DIESEL_TYPE+" VARCHAR(50),
"+DIESEL_RATE+" DOUBLE, "+DIESEL_QUANTITY+" DOUBLE, "+AMOUNT
+" DOUBLE, "+EFFECTIVE_DATE+" VARCHAR(255), "+NO_OF_COPIES
+" VARCHAR(10), "+TIME+" VARCHAR(255), "+TRANSACTION_ID+"
VARCHAR(500), "+REMARK+" VARCHAR(500), "+SYNC+" VARCHAR(20),
"+CLIENT_CODE+" VARCHAR(255))";
private static final String DROP_TABLE_FINALDATA = "Drop table If Exists
"+TABLE_NAME_FINALDATA;
这是根据日期
获取accountname值的查询public Cursor getAccName(String fromdate, String todate) {
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("Select
"+DbListHelper.ACCOUNT_NAME+", "+DbListHelper.UID+
" from " + DbListHelper.TABLE_NAME_FINALDATA + " where
"+DbListHelper.EFFECTIVE_DATE+"between"+DbListHelper.EFFECTIVE_DATE
+ " = '" + fromdate + "' and " + DbListHelper.EFFECTIVE_DATE + " = '" + todate
+ "'", null);
return cursor;
}
这是活动代码
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_records);
spinnerAccountName = (Spinner) findViewById(R.id.spinnerAccountName);
imgBack = (ImageView) findViewById(R.id.imgBack);
etFromDate = (AutoCompleteTextView) findViewById(R.id.editFromDate);
etTodate = (AutoCompleteTextView) findViewById(R.id.editToDate);
txtTotalCount = (TextView) findViewById(R.id.NoOfSlips);
txtdieselQty = (TextView) findViewById(R.id.DieselQuantity);
txtdieselAmnt = (TextView) findViewById(R.id.Amount);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
listHelper = new SqliteVehicleDetails(getBaseContext());
listHelper.open(getBaseContext());
etFromDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_ID);
}
});
etTodate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_ID_1);
}
});
etFromDate.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (cursor.moveToFirst()) {
String fromDate = cursor.getString(cursor.getColumnIndex(DbListHelper.EFFECTIVE_DATE));
String toDate = cursor.getString(cursor.getColumnIndex(DbListHelper.EFFECTIVE_DATE));
cursor = listHelper.getAccName(fromDate, toDate);
int[] to5 = new int[]{android.R.id.text1};
String[] from5 = new String[]{DbListHelper.ACCOUNT_NAME};
final SimpleCursorAdapter adapter5 = new SimpleCursorAdapter(
getBaseContext(),
android.R.layout.simple_list_item_1,
cursor,
from5,
to5
);
adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAccountName.setAdapter(adapter5);
}
}
});
etTodate.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
cursor = listHelper.getFinalData();
if (cursor.moveToFirst()) {
String fromDate = cursor.getString(cursor.getColumnIndex(DbListHelper.EFFECTIVE_DATE));
String toDate = cursor.getString(cursor.getColumnIndex(DbListHelper.EFFECTIVE_DATE));
cursor = listHelper.getAccName(fromDate, toDate);
int[] to5 = new int[]{android.R.id.text1};
String[] from5 = new String[]{DbListHelper.ACCOUNT_NAME};
final SimpleCursorAdapter adapter5 = new
SimpleCursorAdapter(getBaseContext(),
android.R.layout.simple_list_item_1, cursor,
from5, to5);
adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAccountName.setAdapter(adapter5);
}
}
});
@Override
@Deprecated
protected Dialog onCreateDialog ( int id){
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, datePickerListener, year, month, day);
case DATE_PICKER_ID_1:
return new DatePickerDialog(this, datePickerListener1, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
etFromDate.setText(selectedDay + " / " + (selectedMonth + 1) + " / " + selectedYear);
}
};
private DatePickerDialog.OnDateSetListener datePickerListener1 = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
etTodate.setText(selectedDay + " / " + (selectedMonth + 1) + " / " + selectedYear);
}
};
}
答案 0 :(得分:0)
我认为您应该查看日期选择器,因为它仅用于此目的。旋转器用于更多有限选择。你可以在这里使用它,但你需要做很多改革代码才能达到同样的效果。