I want to have 3 spinner in my app that are for date picking like this: Year – Month- Day For example, today is 2015-10-08 and spinners show this date automatically from system: year-spinner shows 2015 Month-spinner shows 10 Day-spinner shows 08 Although users can change them with dropping down and selecting other one. At last these data must be save in my sql-lite data base. I don't know how to them, please help me.thanks!
This is my code:
my app description is after AddVow is clicked, user add some data as number and date.number is taken by edit text and date is taken by spinner(as i describe above)
MyActivity.class:
public class MyActivity extends Activity {
private ListView listView;
private AddVowAdapter adapter;
private DataBaseClass classDB;
private SQLiteDatabase sqlDB;
private ArrayList<String> vowId = new ArrayList<String>();
private ArrayList<String> vow_number = new ArrayList<String>();
private ArrayList<String> vow_start = new ArrayList<String>();
private AlertDialog.Builder build; // options for Edit/Delete
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.ListView);
classDB =new DataBaseClass(this);
// edit and delete code
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view,final int i,final long l) {
// creating dialog
build = new AlertDialog.Builder(MyActivity.this);
//Edit
build.setNegativeButton("ویرایش کردن نذز",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent edit_intent = new Intent(getApplicationContext(), AddVowActivity.class);
edit_intent.putExtra("id", vowId.get(i));
edit_intent.putExtra("number", vow_number.get(i));
edit_intent.putExtra("startDate", vow_start.get(i));
startActivity(edit_intent);
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});
}
//***************** Menu Button for adding VOW
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_vow, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{ super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.add_vow_menu:
Intent intent = new Intent(getApplicationContext(),AddVowActivity.class);
intent.putExtra("update",false);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onResume() {
displayData();
super.onResume();
}
private void displayData() { //displays data from SQLite
sqlDB = classDB.getWritableDatabase();
Cursor mCursor = sqlDB.rawQuery("SELECT * FROM "+DataBaseClass.VOW_TABLE_NAME, null);
vowId.clear();
vow_number.clear();
vow_start.clear();
if (mCursor.moveToFirst()) {
do {
vowId.add(mCursor.getString(mCursor.getColumnIndex(DataBaseClass.VOW_COLUMN_ID)));
vow_number.add(mCursor.getString(mCursor.getColumnIndex(DataBaseClass.VOW_COLUMN_NUMBER)));
vow_start.add(mCursor.getString(mCursor.getColumnIndex(DataBaseClass.VOW_COLUMN_START_DATE)));
} while (mCursor.moveToNext());
}
AddVowAdapter adapter = new AddVowAdapter(this,vowId, vow_number , vow_start);
listView.setAdapter(adapter);
mCursor.close();
}
}
AddVowActivity.class:
public class AddVowActivity extends Activity implements AdapterView.OnItemSelectedListener{
private EditText addNUM;
private Spinner addYEAR;
private Spinner addMONTH;
private Spinner addDAY;
private Button insertBTN;
private Button deleteBTN;
private DataBaseClass classDB;
private SQLiteDatabase sqlDB;
private String id;
private String number;
private String startDate;
private String startDateFormat;
private boolean isUpdate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_vow);
classDB = new DataBaseClass(this);
addNUM = (EditText) findViewById(R.id.addNum);
addYEAR = (Spinner) findViewById(R.id.addYear);
addMONTH = (Spinner) findViewById(R.id.addMonth);
addDAY = (Spinner) findViewById(R.id.addDay);
insertBTN = (Button) findViewById(R.id.insert);
deleteBTN = (Button) findViewById(R.id.deleteBTN);
isUpdate=getIntent().getExtras().getBoolean("vowUpdate");
if(isUpdate){
id=getIntent().getExtras().getString("id");
number=getIntent().getExtras().getString("number");
startDate=getIntent().getExtras().getString("startDate");
addNUM.setText(number);
}
insertBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
number=addNUM.getText().toString().trim();
startDate= startDateFormat.trim();
}
});
//SPINNERS
ArrayList<String> years=new ArrayList<String>();
int minYear=2000;
for(int i=minYear; i<=2020; i++){
years.add(String.valueOf(i));
}
ArrayAdapter<String> yearAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, years);
addYEAR.setAdapter(yearAdapter);
ArrayList<String> months=new ArrayList<String>();
for(int i=1; i<=12; i++){
months.add(String.format("%02d", i));
}
ArrayAdapter<String> monthAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months);
addMONTH.setAdapter(monthAdapter);
ArrayList<String> dates=new ArrayList<String>();
for(int i=1; i<=31; i++){
dates.add(String.format("%02d", i));
}
ArrayAdapter<String> dateAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dates);
addDAY.setAdapter(dateAdapter);
Calendar calendar=Calendar.getInstance();
addDAY.setSelection(calendar.get(Calendar.DATE)-1);
addMONTH.setSelection(calendar.get(Calendar.MONTH));
addYEAR.setSelection(calendar.get(Calendar.YEAR)-minYear);
addDAY.setOnItemSelectedListener(this);
addMONTH.setOnItemSelectedListener(this);
addYEAR.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (view.getId()){
case R.id.addDay:
break;
case R.id.addMonth:// change accordingly dateadapter in based on month and year.
break;
case R.id.addYear:// change accordingly dateadapter in based on month and year.
break;
}
String dayNO=addDAY.getSelectedItem().toString();
String monthNo=addMONTH.getSelectedItem().toString();
String yearNO=addYEAR.getSelectedItem().toString();
Log.d("AddVowActivity", "onItemSelected :"+dayNO+" "+monthNo+" "+yearNO+"");
// store your date format in your database here.
startDateFormat = yearNO+ "-" + monthNo + "-" + dayNO;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
private void saveData(){
sqlDB=classDB.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(DataBaseClass.VOW_COLUMN_NUMBER,number);
values.put(DataBaseClass.VOW_COLUMN_START_DATE,startDate);
System.out.println("");
if(isUpdate)
{
//update database with new data
sqlDB.update(DataBaseClass.VOW_TABLE_NAME, values, DataBaseClass.VOW_COLUMN_ID + "=" + id, null);
}else {
//insert data into database
sqlDB.insert(DataBaseClass.VOW_TABLE_NAME, null, values);
}
//close database
sqlDB.close();
finish();
}
}
AddVowAdapter.class:
public class AddVowAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> VId;
private ArrayList<String> Vnumber;
private ArrayList<String> Vstart;
public ArrayList<Integer> selectedViewsPositions = new ArrayList<Integer>();
public Cursor dbCursor;
public AddVowAdapter(Context c , ArrayList<String> id, ArrayList<String> number , ArrayList<String> start){
context=c;
VId=id;
Vnumber=number;
Vstart= start;
}
@Override
public int getCount() {
return VId.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Holder mHolder;
LayoutInflater layoutInflater;
if (view == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.vow_list_item, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) view.findViewById(R.id.id);
mHolder.txt_VNumber = (TextView) view.findViewById(R.id.number_text);
mHolder.txt_VStart = (TextView) view.findViewById(R.id.start_text);
view.setTag(mHolder);
} else {
mHolder = (Holder) view.getTag();
}
mHolder.txt_id.setText(VId.get(i));
mHolder.txt_VNumber.setText(Vnumber.get(i));
mHolder.txt_VStart.setText(Vstart.get(i));
return view;
}
public class Holder {
TextView txt_id;
TextView txt_VNumber;
TextView txt_VStart;
}
DataBaseClass.class:
public class DataBaseClass extends SQLiteOpenHelper {
static String DATABASE_NAME = "user's vowes";
public static final String VOW_TABLE_NAME = "vowing";
public static final String VOW_COLUMN_ID = "id";
public static final String VOW_COLUMN_NUMBER = "number";
public static final String VOW_COLUMN_START_DATE ="startDate";
public DataBaseClass(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+VOW_TABLE_NAME+" " +
"("+VOW_COLUMN_ID+" INTEGER PRIMARY KEY, "+
VOW_COLUMN_NUMBER+" TEXT, "+
VOW_COLUMN_START_DATE+" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+VOW_TABLE_NAME);
onCreate(db);
}
}
and this is LogCat:
07-19 17:24:23.047 1884-2184/? V/AudioPolicyManagerBase﹕ setOutputDevice() output 2 device 0002 force 0 delayMs 0
07-19 17:24:23.047 1884-2154/? D/AudioHardwareTinyALSA﹕ OutALSA::setDevice: mode = 0, newDevice=0x2, currentDevice=0x2 ,force= 0 07-19 17:24:23.277 1884-16827/? V/AudioPolicyManagerBase﹕ setOutputDevice() output 2 device 0000 force 0 delayMs 184