1

My application has a view pager including three fragments. These are managed by a FragmentPagerAdapter.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Fragment page adapter
        mainFragmentPagerAdapter = new MainFragmentPagerAdapter(getSupportFragmentManager());

        // Set the view pagers adapter
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(mainFragmentPagerAdapter);

        // Set up the TabLayout using the view pager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.fixed_tabs);
        tabLayout.setupWithViewPager(viewPager);
}

Things start to get complicated when all of these Fragments contain a RecyclerView that can be updated.

To correctly pass data to the Fragments, I'm using the newInstance method shown below:

public static InformationFragment newInstance() {
        Bundle arguments = new Bundle();
        // Put arguments here.
        InformationFragment fragment = new InformationFragment();
        fragment.setArguments(arguments);
        return fragment;
}

I then have another method inside InformationFragment to update the arguments when required:

public void updateData() {

    Bundle arguments = getArguments();
    // Put arguments
    this.adapter.updateItems(items);
}

Data that can be used to populate the RecyclerView needs to be of multiple types (some android, some custom). Therefore, I have ArrayList<Object> items; containing all my objects.

My question is, what is the best way to store this ArrayList in a bundle so it can be used by a fragment?

I am able to do the following, but it feels dirty:

public void updateData(ArrayList<Object> items) {
        getArguments().putSerializable("items", items);
        this.adapter.updateItems(items);
    }

and then retrieve by:

if (arguments.containsKey("items")) {
            ArrayList<Object> items = (ArrayList<Object>)arguments.getSerializable("items");
}

Or is there a better approach?


SimpleMembership - object 'Users' already exists

I'm developing SPA with Asp.net MVC. Project works on localhost fine, but i've a problem on hosting.

There is controller to work with MembershipProvider containg few methods.

[InitializeSimpleMembership]
public class AccountController : ApiController
{
    public object Get()
    {
        ...
    }

    ...
}

The key moment - is the attribute InitializeSimpleMembership. It's declaration:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        // Ensure ASP.NET Simple Membership is initialized only once per app start
        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
    }

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer(new ITAInitializer());

            using (var context = new ITAContext())
            {
                if (!context.Database.Exists())
                {
                    ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                }
            }

            WebSecurity.InitializeDatabaseConnection("ITAContext", "Users", "ID", "Login", autoCreateTables: true);

            if (!Roles.RoleExists(UserRights.Admin.ToString()))
                Roles.CreateRole(UserRights.Admin.ToString());

            if (!Roles.RoleExists(UserRights.Moderator.ToString()))
                Roles.CreateRole(UserRights.Moderator.ToString());

            if (!Roles.RoleExists(UserRights.User.ToString()))
                Roles.CreateRole(UserRights.User.ToString());
        }
    }
}

When i'm calling any method in controller i'm getting an error:

There is already an object named 'Users' in the database.

If i'm deleting table Users then first request running fine and creating the table. In the next call i'm getting an error again. Why it's trying to create table and not using existing?

4

1 に答える 1

0

List<Object>を保持せず、代わりに複数のリストをバンドルに入れることをお勧めします。また、オブジェクトをシリアライズ可能としてバンドルに入れることはそれほど悪いことではありませんが、よりアンドロイド的な方法で行いたい場合は、そのバンドル内に格納されている各オブジェクトに対して Parcelable インターフェイスを実装できます。

于 2015-08-13T16:43:50.443 に答える