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/view/get_brands.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_selection);
Bundle b = getIntent().getExtras();
if(b != null){
String selection1=b.getString("pickup");
String selection2=b.getString("pickdate");
String selection3=b.getString("picktime");
String selection4=b.getString("dropoff");
String selection5=b.getString("dropdate");
String selection6=b.getString("droptime");
TextView text1=(TextView)findViewById(R.id.pickup);
TextView text2=(TextView)findViewById(R.id.pickdate);
TextView text3=(TextView)findViewById(R.id.picktime);
TextView text4=(TextView)findViewById(R.id.dropoff);
TextView text5=(TextView)findViewById(R.id.dropdate);
TextView text6=(TextView)findViewById(R.id.droptime);
text1.setText(selection1);
text2.setText(selection2);
text3.setText(selection3);
text4.setText(selection4);
text5.setText(selection5);
text6.setText(selection6);
}
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();
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinnerbrand);
spinner1.setOnItemSelectedListener(new CarBrandSelection(this));
}
...
}
CarBrandSelection.java:
public class CarBrandSelection implements OnItemSelectedListener {
private TextView pdestination, pdate, ptime, ddestination, ddate, dtime;
Activity mActivity;
public CarBrandSelection(Activity activity) {
mActivity = activity;
}
public void onClick(View v) {
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
switch(pos){
case 1:
pdestination=(TextView) findViewById(R.id.pickup);
pdate=(TextView) findViewById(R.id.pickdate);
ptime=(TextView) findViewById(R.id.picktime);
ddestination=(TextView) findViewById(R.id.dropoff);
ddate=(TextView) findViewById(R.id.dropdate);
dtime=(TextView) findViewById(R.id.droptime);
Bundle b=new Bundle();
b.putString("destination", pdestination.getText().toString());
Intent intent = new Intent(mActivity, FirstListView.class);
intent.putExtras(b);
mActivity.startActivity(intent);
break;
case 2:
Intent intent1 = new Intent(mActivity, SecondListView.class);
mActivity.startActivity(intent1);
break;
case 3:
Intent intent2 = new Intent(mActivity, ThirdListView.class);
mActivity.startActivity(intent2);
break;
case 4:
Intent intent3 = new Intent(mActivity, FourthListView.class);
mActivity.startActivity(intent3);
break;
// and so on
// .....
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
FirstListView.java:
public class FirstListView extends Activity implements FetchDataListener{
private ProgressDialog dialog;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Bundle b = getIntent().getExtras();
String selection1=b.getString("destination");
TextView text1=(TextView)findViewById(R.id.p_destination);
text1.setText(selection1);
initView();
addListenerOnListItemSelection();
}
main_activity.xml
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="235dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"/>
<LinearLayout
android:id="@+id/linealayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/p_destination"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/drop_off" />
</LinearLayout>
activity_car_selection.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CarSelection" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vehicle_brand"/>
<TextView android:id="@+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/pickdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/picktime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/dropoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/dropdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView android:id="@+id/droptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
我真的需要帮助来解决这个问题。我希望用户点击微调器(R.id.spinnerbrand
)中的特定项时,textview(R.id.pickup
)中的数据将传递到下一个活动FirstListView.java
。我不确定我是否正确执行,因为无法传递数据。真的很感激,如果有人可以帮助解决这个问题。非常感谢
答案 0 :(得分:0)
如果您想从微调器获取数据,可以使用:
String selected = parent.getItemAtPosition(pos).toString();
为了将数据传递给其他活动,基本上这就是你需要做的事情:
在第一个活动中,您应该创建一个Intent
设置操作并添加您需要的内容:
Intent intent = new Intent();
intent.setAction(this, SecondActivity.class);
intent.putExtra(tag, value);
startActivity(intent);
并在第二项活动中:
Intent intent = getIntent();
intent.getBooleanExtra(tag, defaultValue);
intent.getStringExtra(tag, defaultValue);
intent.getIntegerExtra(tag, defaultValue);
其中一个get函数会返回给你的值,具体取决于你经过的数据类型。
或在第二个活动中,例如我想获取我包含的名称的值:
第一堂课时intent.putExtra("name", name);
,你可以简单地做:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");