how can i store the selected item from the spinner to the database.. my spinner list of item is declared in the resources..and i just call it by using getresources() .. also i want to display the it to another activity how can i do it?
public class ProjectDetail extends Activity implements View.OnClickListener {
Button btnsave, btndelete, btnclose;
EditText ettitle, etType, etprio, ettf;
EditText etsd, eted,etcost,etstat;
private int _Project_Id=0;
Spinner spinptype, spinpprio;
Spinner spinpstat, spinptf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_detail);
btnsave = (Button) findViewById(R.id.btnSave);
btndelete = (Button) findViewById(R.id.btnClose);
btnclose = (Button) findViewById(R.id.btnClose);
ettitle = (EditText) findViewById(R.id.eTtitle);
// etType = (EditText) findViewById(R.id.eTtype);
// etprio = (EditText) findViewById(R.id.eTprio);
// ettf = (EditText) findViewById(R.id.eTtf);
etsd = (EditText) findViewById(R.id.eTsd);
eted = (EditText) findViewById(R.id.eTed);
etcost = (EditText) findViewById(R.id.eTcost);
// etstat = (EditText) findViewById(R.id.eTstat);
spinpprio = (Spinner) findViewById(R.id.spinprio);
String[] priolist = getResources().getStringArray(R.array.prioritylist);
int prioindex = 0;
for(String prioitem : priolist) {
if(prioitem.equals("your_item")){
// index++;
//break;
}
prioindex++;
}spinpprio.setSelection(prioindex -1);
spinpstat = (Spinner) findViewById(R.id.spinstatus);
String[] statlist = getResources().getStringArray(R.array.statuslist);
int statindex = 0;
for(String statitem : statlist) {
if(statitem.equals("your_item")){
// index++;
break;
}
statindex++;
}spinpstat.setSelection(statindex -1);
spinptf = (Spinner) findViewById(R.id.spintf);
String[] tflist = getResources().getStringArray(R.array.prioritylist);
int tfindex = 0;
for(String tfitem : tflist) {
if(tfitem.equals("your_item")){
break;
}
tfindex++;
}spinptf.setSelection(tfindex -1);
spinptype = (Spinner) findViewById(R.id.spinptype);
String[] ptlist = getResources().getStringArray(R.array.prioritylist);
int ptindex = 0;
for(String ptitem : ptlist) {
if(ptitem.equals("your_item")){
// index++;
break;
}
ptindex++;
}spinptype.setSelection(ptindex -1);
_Project_Id=0;
Intent intent = getIntent();
_Project_Id = intent.getIntExtra("project_Id", 0);
ProjectCrud pcrud = new ProjectCrud(this);
Project project = new Project();
project = pcrud.getProjectById(_Project_Id);
ettitle.setText(project.title);
// etType.setText(project.type);
// etprio.setText(String.valueOf(project.priority));
// ettf.setText(project.timeframe);
etsd.setText(project.start);
eted.setText(project.end);
etcost.setText(String.valueOf(project.cost));
// etstat.setText(project.status);
spinptype.setSelection(ptindex -1);
spinptf.setSelection(tfindex -1);
spinpstat.setSelection(statindex -1);
spinpprio.setSelection(prioindex -1);
//what is the right way to set the selected item from spinner?
}
@Override
public void onClick(View view) {
if (view == findViewById(R.id.btnSave)){
ProjectCrud pcrud = new ProjectCrud(this);
Project project = new Project();
project.title=ettitle.getText().toString();
// project.type=etType.getText().toString();
// project.priority= Integer.parseInt(etprio.getText().toString());
// project.timeframe=ettf.getText().toString();
project.start=etsd.getText().toString();
project.end=eted.getText().toString();
project.cost=Integer.parseInt(etcost.getText().toString());
// project.status=etstat.getText().toString();
//==========> Now here how can i get the value from selected from spinner
project.project_ID=_Project_Id;
if(_Project_Id==0){
_Project_Id=pcrud.insert(project);
Toast.makeText(this, "New Project Created", Toast.LENGTH_SHORT).show();
}else {
pcrud.update(project);
Toast.makeText(this,"Project Updated", Toast.LENGTH_SHORT).show();
}
}else if (view == findViewById(R.id.btnDelete)){
ProjectCrud pcrud = new ProjectCrud(this);
pcrud.delete(_Project_Id);
Toast.makeText(this,"Project Deleted", Toast.LENGTH_SHORT).show();
finish();
}else if (view == findViewById(R.id.btnClose)){
finish();
}
}
@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_project_detail, 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);
}
}
this my declaration for the item on each spinner .. i have 4 spinner in one activity .
<string-array name = "timeframelist">
<item>3</item>
<item>6</item>
<item>12</item>
</string-array>
<string-array name="prioritylist">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
<string-array name="statuslist">
<item>Finished</item>
<item>On-going</item>
<item>Pending</item>
</string-array>
<string-array name="typelist">
<item>Website</item>
<item>Web-Portal</item>
<item>Web Application</item>
</string-array>
<string-array name="comstandlist">
<item>Old</item>
<item>New</item>
<item>Null</item>
</string-array>
and this my code for the crud operation.. do i need to change it cause i change my EditText to Spinner..? How can i do it? can i have some example ?
public class ProjectCrud {
private DBHelper dbHelper;
public ProjectCrud(Context context){
dbHelper = new DBHelper(context);
}
public int insert(Project project){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Project.KEY_title, project.title);
values.put(Project.KEY_type, project.type);
values.put(Project.KEY_priority, project.priority);
values.put(Project.KEY_timeframe, project.timeframe);
values.put(Project.KEY_start, project.start);
values.put(Project.KEY_end, project.end);
values.put(Project.KEY_cost, project.cost);
values.put(Project.KEY_status, project.status);
long project_Id = db.insert(Project.TABLE, null, values);
db.close();
return (int) project_Id;
}
public void delete(int project_Id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(Project.TABLE, Project.KEY_ID + "=?", new String[]{ String.valueOf(project_Id) });
db.close();
}
public void update(Project project){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Project.KEY_title, project.title);
values.put(Project.KEY_type, project.type);
values.put(Project.KEY_priority, project.priority);
values.put(Project.KEY_timeframe, project.timeframe);
values.put(Project.KEY_start, project.start);
values.put(Project.KEY_end, project.end);
values.put(Project.KEY_cost, project.cost);
values.put(Project.KEY_status, project.status);
db.update(Project.TABLE, values, Project.KEY_ID + "= ?", new String[]{String.valueOf(project.project_ID)});
db.close();
}
public ArrayList<HashMap<String, String>> getProjectList (){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Project.KEY_ID + "," +
Project.KEY_title + "," +
Project.KEY_type + "," +
Project.KEY_priority + "," +
Project.KEY_timeframe + "," +
Project.KEY_start + "," +
Project.KEY_end + "," +
Project.KEY_cost + "," +
Project.KEY_status +
" FROM " + Project.TABLE;
ArrayList<HashMap<String, String>> projectList = new ArrayList<HashMap<String, String>>();
Cursor cursor= db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do {
HashMap<String , String > project = new HashMap<String, String >();
project.put("id", cursor.getString(cursor.getColumnIndex(Project.KEY_ID)));
project.put("title", cursor.getString(cursor.getColumnIndex(Project.KEY_title)));
projectList.add(project);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return projectList;
}
public Project getProjectById(int pId){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Project.KEY_ID + "," +
Project.KEY_title + "," +
Project.KEY_type + "," +
Project.KEY_priority + "," +
Project.KEY_timeframe + "," +
Project.KEY_start + "," +
Project.KEY_end + "," +
Project.KEY_cost + "," +
Project.KEY_status +
" FROM " + Project.TABLE
+ " WHERE " +
Project.KEY_ID + "=?";
int iCount =0;
Project project = new Project();
Cursor cursor = db.rawQuery(selectQuery, new String[]{ String.valueOf(pId)});
if (cursor.moveToFirst()){
do{
project.project_ID = cursor.getInt(cursor.getColumnIndex(Project.KEY_ID));
project.title = cursor.getString(cursor.getColumnIndex(Project.KEY_title));
project.type = cursor.getString(cursor.getColumnIndex(Project.KEY_type));
project.priority = cursor.getInt(cursor.getColumnIndex(Project.KEY_priority));
project.timeframe = cursor.getString(cursor.getColumnIndex(Project.KEY_timeframe));
project.start = cursor.getString(cursor.getColumnIndex(Project.KEY_start));
project.end = cursor.getString(cursor.getColumnIndex(Project.KEY_end));
project.cost = cursor.getInt(cursor.getColumnIndex(Project.KEY_cost));
project.status = cursor.getString(cursor.getColumnIndex(Project.KEY_status));
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return project;
}
}
can someone explain all the change i need to do with my project.. any idea about on how i can separate all the users who is admin from client.. i have login module and when button login is click how can i determined if the user who login is an admin or a client.. for admin that user has all the priveledge to create edit and delete anything in the database while the users in client view can only view all the data.. how can i do it?
Aucun commentaire:
Enregistrer un commentaire