Spinner选择项目并将片段设置为活动

时间:2016-02-13 17:42:20

标签: android android-fragments

活动代码:

public class UsersActivity extends AppCompatActivity {
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int idOfUser;
        DatabaseHandler db = new DatabaseHandler(this);
        Intent i = getIntent();
        idOfUser = i.getIntExtra("id", 0);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_users);
        TextView tvOfHello = (TextView) findViewById(R.id.tvHelloInUserCabinet);
      NewUserTable newUserTable =  db.getUser(idOfUser);
        tvOfHello.setText("Здравствуйте " + String.valueOf(newUserTable.getName()) + "\nВаш баланс : " + String.valueOf(newUserTable.getBalance()));

    }
}
/*
if(spinner.getSelectedItem().equals("Корзина")){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ShoppingBasket shopBask = new ShoppingBasket();
        fragmentTransaction.add(R.id.ShoppingBasketFragment,shopBask,"HELLO");
        fragmentTransaction.commit();
        }*/

如何从spinner的事件处理程序中将片段设置为activity?选择项目=更改活动中的片段。感谢

2 个答案:

答案 0 :(得分:0)

为spinner

定义此侦听器
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    // your code here
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // your code here
}

});

请注意,在构建视图时也会调用onItemSelected方法,因此您可以考虑将其放在onCreate()方法调用中。

并更改片段。

private void changeFragment(String spinnerSelectedItem) {

 FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ShoppingBasket shopBask = new ShoppingBasket();
    fragmentTransaction.add(R.id.ShoppingBasketFragment,shopBask,"HELLO");
    fragmentTransaction.commit();
    }
}

在onCreate里面调用上面的方法。 也可以从spinnerOnItemSelectedListener调用上面的方法

希望这有帮助。

答案 1 :(得分:0)

public class UsersActivity extends AppCompatActivity {
Spinner spinner = (Spinner) findViewById(R.id.spinner);
@Override
protected void onCreate(Bundle savedInstanceState) {
    int idOfUser;
    DatabaseHandler db = new DatabaseHandler(this);
    Intent i = getIntent();
    idOfUser = i.getIntExtra("id", 0);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users);
    TextView tvOfHello = (TextView) findViewById(R.id.tvHelloInUserCabinet);
  NewUserTable newUserTable =  db.getUser(idOfUser);
    tvOfHello.setText("Здравствуйте " + String.valueOf(newUserTable.getName()) + "\nВаш баланс : " + String.valueOf(newUserTable.getBalance()));

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            changeFragment("Корзина");
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });
}
private void changeFragment(String spinnerSelectedItem) {

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ShoppingBasket shopBask = new ShoppingBasket();
    fragmentTransaction.add(R.id.ShoppingBasketFragment, shopBask, "HELLO");
    fragmentTransaction.commit();
}

}和例外