I'd like to inquire about the Android adapter.

Asked 2 years ago, Updated 2 years ago, 122 views

I just started studying Android, so I have a question.

While trying to put a list view inside each menu made with a view phaser, I'm wondering how to handle the list view-related adapter.

I've seen a lot of cases where the adapter is an internal class within an activity that contains the onCreate() method.

Below is the menu adapter using the view page in the main activity I made.

private class MenuPagerAdapter extends PagerAdapter {

    private LayoutInflater menuLayoutInflater;
    private ListView listView;

    public MenuPagerAdapter(Context context) {
        menuLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = null;

        switch (position){
            case 0 :
                view = menuLayoutInflater.inflate(R.layout.activity_menu1, null);
                break;
            case 1 :
                view = menuLayoutInflater.inflate(R.layout.activity_menu2, null);
                break;
            case 2 :
                view = menuLayoutInflater.inflate(R.layout.activity_menu3, null);
                break;
        }

        container.addView(view);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}

Of course, there are many ways.

Is it better to create and manage adapter folders separately?

I wonder if it is a good case to manage it as an internal class within the activity as I have done.

It's not a good case, but I'm asking you because I'm curious about how many people do it.

adapter

2022-09-22 22:07

1 Answers

It depends. In my case, if the adapter class is not that long, I defined it inside and used it If there were many codes in the adapter, it was taken out of the internal class and used.

There was a time when I developed an Android app by applying the mvc pattern. At this time, the class that plays the role of view defined adapter as an internal class and used it a lot.

If you go to the address below, there is a pdf that explains how to apply mvc to Android. http://www.androidpub.com/2437353See also I hope it helps you.


2022-09-22 22:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.