簡體   English   中英

如何將數據從textview傳遞到listview項目選擇

[英]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>

我真的需要幫助來解決這個問題。 我想做的是,當用戶單擊微調器( R.id.spinnerbrand )中的特定項目時,來自textview( R.id.pickup )的數據將傳遞到下一個活動,即FirstListView.java 我不確定是否正確執行此操作,因為無法傳遞數據。 非常感謝有人可以幫助解決此問題。 非常感謝

如果要從微調器中獲取數據,可以使用:

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

為了將數據傳遞到其他活動,基本上這是您需要做的:
在第一個活動中,您應該創建一個Intent設置Action並添加所需的內容:

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);的name的值: intent.putExtra("name", name); 在頭等艙,您可以簡單地進行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM