【问题标题】:Move Activity to Fragment将活动移动到片段
【发布时间】:2017-03-31 07:12:59
【问题描述】:

求助,这个代码(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();
    }
}

【问题讨论】:

  • 好吧,阅读我建议的片段。你一定会找到解决办法的。

标签: android android-fragments android-activity


【解决方案1】:

您的代码中有很多问题。

  • 不要在 onResume() 中执行此类操作。 setContentView(..) 应该在 Activity onCreate(..) 回调中完成。在一些特殊场景下,setContentView(..) 可以根据不同的条件进行。
  • 可能从Android资源入手比较好,Google有很多样例。 Google samples for Android
  • 请阅读安卓文档Android Docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多