简体   繁体   English

如何将数据从textview传递到listview项目选择

[英]how to pass data from textview to listview items selection

CarSelection.java: 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: 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: 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 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 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 . 我想做的是,当用户单击微调器( R.id.spinnerbrand )中的特定项目时,来自textview( R.id.pickup )的数据将传递到下一个活动,即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设置Action并添加所需的内容:

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. 其中一个get函数将根据您传递的数据类型为您返回值。

or in the second activity, for example i want to get the value of name that I include at: intent.putExtra("name", name); 或在第二个活动中,例如,我想获取包含在以下intent.putExtra("name", name);的name的值: 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将listview项中的随机数据添加到Textview中 - Add random data from listview items into Textview 如何在ListView项目中进行操作并将该数据传递到android中的下一个活动? - How to do operations in ListView items and to pass that data to next activity in android? 如何将ListView中的TextView传递回PHP - How to pass the textview in listview back to php 如何将ListView切换为TextView以从数据库检索数据 - How to switch ListView to TextView to retrieve data from database 在 Android 中,给定带有 TextView 和 RadioGroup 的项目的 ListView,如何动态添加项目? - In Android, given a ListView of items with TextView and RadioGroup, how to add items dynamically? 如何为ListView中的项目设置onClickListener(通过从Firebase检索数据来动态生成Listview的项目) - How to set onClickListener for items in a ListView (items of listview are generated dynamically by retreiving data from firebase) 在ListView中更改单个项目的TextView - Changing TextView of individual items in a ListView 如何在所有选中的项目中获取作为 ListView 行一部分的 TextView 的值 - How to get the value of a TextView that is part of a ListView row in all checked items 如何从hashmap用2 textview设置listview - how to set listview with 2 textview from hashmap 如何从ListView项目获取TextView? - How to get TextView from ListView item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM