简体   繁体   中英

how to pass data from textview to listview items selection

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>

I really need help to solve this problem. I want to do like when a user clicks on a specific item in the spinner ( R.id.spinnerbrand ), the data from the textview ( R.id.pickup ) will be passed to the next activity which is FirstListView.java . I'm not sure whether I do it correctly because the data cannot be passed. Really appreciate if someone can help to solve this. Thanks a lot

If you want to get the data from the spinner you can use:

String selected = parent.getItemAtPosition(pos).toString();

And for passing the data to the other activity basically this is what you need to do:
in the first activity you should create a Intent set the Action and add what you need:

Intent intent = new Intent();
intent.setAction(this, SecondActivity.class);
intent.putExtra(tag, value);
startActivity(intent);

and in the second activity:

Intent intent = getIntent();
intent.getBooleanExtra(tag, defaultValue);
intent.getStringExtra(tag, defaultValue);
intent.getIntegerExtra(tag, defaultValue);

one of the get-functions will give return you the value, depending on the datatype you are passing through.

or in the second activity, for example i want to get the value of name that I include at: intent.putExtra("name", name); on first class, for that you can simply make:

 Bundle b = new Bundle();
 b = getIntent().getExtras();
 String name = b.getString("name");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM