将活动移动到片段

时间:2017-03-31 07:12:59

标签: android android-fragments android-activity

帮助,如何将此代码(Activity)传递给代码(Fragment),它应该如何?

public class SelectProfileActivity extends AppCompatActivity implements ProfileAdapter.ProfileAdapterItemCallBack {

    private List<Profile> Profiles;
    private RecyclerView morral;
    private GridLayoutManager correa;
    private ProfileAdapter adapter;
    private SharedPreferencesController spc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    public void crearPerfil(View view){
        Intent inten=new Intent(this,RegistrarPerfil.class);
        startActivity(inten);
    }
    @Override
    protected void onResume() {
        super.onResume();
        spc = new SharedPreferencesController(this);
        setContentView(R.layout.activity_select_profile);
        //la lista donde se agregaran los perfiles
        Profiles = new ArrayList<Profile>();
        //Consulta para obtener todos los perfiles
        Profiles=App.getProfFromUser(spc.getPrefUID());
        adapter = new ProfileAdapter(Profiles, this);
        if (Profiles.isEmpty()){
            App.showMessage("No se encontraron Perfiles",this);
        }
        else
        {
            //RecyclerView donde se van a mostrar los elementos
            morral = (RecyclerView)findViewById(R.id.rv_all_profiles);
            correa = new GridLayoutManager(this,3);
            morral.setLayoutManager(correa);
            morral.setAdapter(adapter);
        }

    }

    @Override
    public void onProfileItemSelected(Profile perfil) {
        spc.saveProfile(perfil);
        Intent inte = new Intent(this, CategoryActivity.class);
        startActivity(inte);
        finish();
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码中出现了很多问题。

  • 不要在onResume()中执行此类操作。 setContentView(..)应该在Activity onCreate(..)回调中完成。在某些特殊情况下,可以根据不同的条件完成setContentView(..)。
  • 从Android资源开始可能是好事,谷歌有很多样本。 Google samples for Android
  • 请阅读android docs Android Docs