MyMainScreen.java
public class MyMainScreen extends Activity implements OnClickListener{
// Widget GUI
Button btnCalendar, btnCalendar1, btnTime, btnTime1;
private Spinner spinner1, spinner2;
private ArrayList<Category> categoriesList;
ProgressDialog pDialog;
// API urls
// Url to get all categories
private String URL_CATEGORIES = "http://10.0.2.2/car_rental/get_categories.php";
// Variable for storing current date and time
private int mYear, mMonth, mDay, mHour, mMinute;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnSpinnerItemSelection();
btnCalendar = (Button) findViewById(R.id.btnCalendar);
btnTime = (Button) findViewById(R.id.btnTime);
btnCalendar1 = (Button) findViewById(R.id.btnCalendar1);
btnTime1 = (Button) findViewById(R.id.btnTime1);
btnCalendar.setOnClickListener(this);
btnTime.setOnClickListener(this);
btnCalendar1.setOnClickListener(this);
btnTime1.setOnClickListener(this);
categoriesList = new ArrayList<Category>();
new GetCategories().execute();
}
/**
* Async task to get all food categories
* */
private class GetCategories extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MyMainScreen.this);
pDialog.setMessage("Fetching data..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("pickup");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("id"),
catObj.getString("name"));
categoriesList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner();
}
}
/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(spinnerAdapter);
spinner2.setAdapter(spinnerAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinnerpick);
spinner1.setOnItemSelectedListener(new ItemSelectedListener());
spinner2 = (Spinner) findViewById(R.id.spinnerdrop);
spinner2.setOnItemSelectedListener(new ItemSelectedListener());
}
@Override
public void onClick(View v) {
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in text box
btnCalendar.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
if (v == btnTime) {
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in text box
btnTime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd.show();
}
if (v == btnCalendar1) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd1 = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in text box
btnCalendar1.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd1.show();
}
if (v == btnTime1) {
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd1 = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in text box
btnTime1.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd1.show();
}
}
public void newActivity(View view){
Bundle b=new Bundle();
b.putString("pickup", spinner1.getItemAtPosition(spinner1.getSelectedItemPosition()).toString());
b.putString("dropoff", spinner2.getItemAtPosition(spinner2.getSelectedItemPosition()).toString());
Intent alarmSet = new Intent(getApplicationContext(), CarSelection.class);
alarmSet.putExtras(b);
startActivityForResult(alarmSet, 0);
}
}
CarSelection.java
public class CarSelection extends Activity {
private Spinner spinner1;
// array list for spinner adapter
private ArrayList<Category> brandsList;
ProgressDialog pDialog;
// Url to get all categories
private String URL_CATEGORIES = "http://10.0.2.2/car_rental/get_brands.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_selection);
Bundle b=this.getIntent().getExtras();
if(b != null){
String selection1=b.getString("pickup");
String selection2=b.getString("dropoff");
TextView text1=(TextView)findViewById(R.id.pickup);
TextView text2=(TextView)findViewById(R.id.dropoff);
text1.setText(selection1);
text2.setText(selection2);
}
else{
Toast.makeText(CarSelection.this,"Haven't Received any data yet", Toast.LENGTH_LONG).show();
}
brandsList = new ArrayList<Category>();
// Show the Up button in the action bar.
setupActionBar();
addListenerOnSpinnerItemSelection();
new GetCategories().execute();
}
/**
* Async task to get all food categories
* */
private class GetCategories extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CarSelection.this);
pDialog.setMessage("Fetching data..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("brands");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("id"),
catObj.getString("name"));
brandsList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner();
}
}
/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < brandsList.size(); i++) {
lables.add(brandsList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(spinnerAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinnerbrand);
spinner1.setOnItemSelectedListener(new CarBrandSelection(this));
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.car_selection, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_up" />
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinnerpick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
/>
<Button
android:id="@+id/btnCalendar"
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Calendar" />
<Button
android:id="@+id/btnTime"
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/time_picker" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="@string/drop_off" />
<LinearLayout
android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinnerdrop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<Button
android:id="@+id/btnCalendar1"
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Calendar" />
<Button
android:id="@+id/btnTime1"
style="?android:attr/spinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/time_picker" />
</LinearLayout>
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/button_send"
android:onClick="newActivity"/>
</RelativeLayout>
问题是我的DatePicker和TimePicker实际上是按钮。我希望在用户选择时间和日期并按下一个按钮后,日期和时间的值将显示在新活动的文本视图中。您可以引导我完成此操作。谢谢。
答案 0 :(得分:0)
用这个&#34; put&#34;文件......
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
startActivity(i);
然后,要检索值,请尝试以下内容:
Bundle extras = getIntent().getExtras();
String newString= extras.getString("STRING_I_NEED");