我有一个自定义微调器,在活动中定义了一个ArrayList。在所选项目上,将创建一个新片段。我可以将创建了哪个片段的值(“Single Massage”或“Couples Massage”)传递给同一活动中的另一个片段,然后从该片段传递给另一个活动并更新其textview?这可能不是最好的方法,但我是初学者,所以我真的不知道那么多。这是我的代码。
微调:
public class MassageActivity extends AppCompatActivity {
private Spinner mSpinner;
private Toolbar mToolbar;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_massage);
mToolbar = (Toolbar) findViewById(R.id.toolbar_massage);
setSupportActionBar(mToolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
if (ab != null) {
ab.setDisplayShowTitleEnabled(false);
}
customSpinner();
mFragmentManager = getSupportFragmentManager();
}private void customSpinner() {
final ArrayList<String> massageType = new ArrayList<>();
massageType.add("Single Massage");
massageType.add("Couples Massage");
massageType.add("Pamper Party");
mSpinner = (Spinner) findViewById(R.id.spinner);
CustomSpinnerAdapter spinnerAdapter = new CustomSpinnerAdapter(getApplicationContext(), massageType);
mSpinner.setAdapter(spinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).toString()
.equals("Pamper Party")) {
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container_massage,
new PamperPartyFragment()).commit();
}
if (parent.getItemAtPosition(position).toString()
.equals("Single Massage")) {
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container_massage,
new SingleMassageFragment()).commit();
}
if (parent.getItemAtPosition(position).toString()
.equals("Couples Massage")){
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container_massage,
new CouplesMassageFragment()).commit();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}}
编辑: 我跟着并完成了这个:
String spinnerValue = mSpinner.getSelectedItem().toString();
Intent i = new Intent(this, SingleMassageFragment.class);
i.putExtra("spinnerValue", spinnerValue);
setIntent(i);
然后在我的片段中:
MassageActivity activity = (MassageActivity) getActivity();
Intent f = activity.getIntent();
String spinnerValue = f.getStringExtra("spinnerValue");
Utils.doToast(getContext(), spinnerValue);
但无论我选择哪个片段,我总是得到同样的吐司“单身按摩”。这是为什么?对于任何情况,这是我的适配器:
public class CustomSpinnerAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mMassageType;
private LayoutInflater mInflater;
private TextView tvSpinner;
public CustomSpinnerAdapter(Context context, ArrayList<String> massageType) {
mContext = context;
mMassageType = massageType;
mInflater = (LayoutInflater.from(context));
}
@Override
public int getCount() {
return mMassageType.size();
}
@Override
public Object getItem(int position) {
return mMassageType.get(position);
}
@Override
public long getItemId(int position) {
return (long) position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.spinner_layout, null);
tvSpinner = (TextView) convertView.findViewById(R.id.tvSpinner);
tvSpinner.setText(mMassageType.get(position));
return convertView;
}
}
答案 0 :(得分:1)
传递数据
PamperPartyFragment mPamperPartyFragment = new PamperPartyFragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
mPamperPartyFragment.setArguments(bundle);
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container_massage,
mPamperPartyFragment ).commit();
用于获取片段中的数据
Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);
在您的微调器setOnItemSelectedListener中尝试此操作以发送数据
Bundle bundle = new Bundle();
bundle.putString("spinnerValue",parent.getItemAtPosition(position).toString());
mPamperPartyFragment.setArguments(bundle);
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment_container_massage,
mPamperPartyFragment ).commit();
要阅读您的片段中的数据,请在onCreate
Bundle bundle = getArguments();
String selectedVAlue = bundle.getString("spinnerValue");
答案 1 :(得分:1)
in activity :
Intent i = new Intent (this, fragmentname.class);
i.putInt(key, value);
setIntent(i);
in fragment :
ActivityName activity = (ActivityName) getActivity();
Intent f= activity.getIntent();
int yrid = f.getIntExtra(key);