Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Custom ListView Adapter from Fragment Class Main Activity

I've written a few listView activities, as a proof of concept for myself that I could do it. Now, I'm having trouble loading a listView activity into a single tab for an app with multiple tabs, that allows both swiping and tab selection for navigation. I get the error "The constructor ListView_Adapter(MainActivity.DummySectionFragment) is undefined" when I try to write the code for it. I'm a beginner, and I've lurked pretty hard here for the past few days. Any help is appreciated.

TL;DR : I'm a n00b, and I can't figure out this problem.

My Custom List Adapter

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    public class CustomListViewAdapter extends ArrayAdapter<String> {

        public CustomListViewAdapter (Context c) {
            super(c, R.layout.list_cell);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ListView_Text holder = null;

            if (row == null)
            {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                row = inflater.inflate(R.layout.list_cell, parent, false);
                holder = new ListView_Text(row);
                row.setTag(holder);
            }
            else
            {
                holder = (ListView_Text) row.getTag();
            }

            holder.populateFrom(getItem(position));

            return row;
        }

        static class ListView_Text {
            private TextView cell_name = null;

            ListView_Text(View row) {
                cell_name = (TextView) row.findViewById(R.id.list_cell_name);
            }

            void populateFrom(String index) {
                cell_name.setText(index);
            }

        }
    }

My Main Activity

    import java.util.Locale;

    import android.app.ActionBar;
    import android.app.ActionBar.Tab;
    import android.app.FragmentTransaction;
    import com.example.twigglebeta2.ListView_Adapter;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.app.NavUtils;
    import android.support.v4.view.ViewPager;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
    import android.widget.TextView;

    public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
        private static ListView_Adapter listViewAdapter;
        private ListView listView;

        SectionsPagerAdapter mSectionsPagerAdapter;

        ViewPager mViewPager;

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

            // Set up the action bar.
            final ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // Create adapter
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

            // Switch tab selection to match current page when swiped
            mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });

            // Create a tab for each 'count' in the activity from getCount()
            for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
                Tab thisTab = actionBar.newTab();
                thisTab.setText(mSectionsPagerAdapter.getPageTitle(i));
                thisTab.setTabListener(this);
                actionBar.addTab(thisTab);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
            // When the given tab is selected, switch to the corresponding page in
            // the ViewPager.
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
        }

        /**
         * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
         * one of the sections/tabs/pages.
         */
        public class SectionsPagerAdapter extends FragmentPagerAdapter {

            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                // getItem is called to instantiate the fragment for the given page.
                // Return a DummySectionFragment (defined as a static inner class
                // below) with the page number as its lone argument.
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;
            }

            @Override
            public int getCount() {
                // Show 3 total pages.
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
                }
                return null;
            }
        }

        /**
         * A dummy fragment representing a section of the app, but that simply
         * displays dummy text.
         */
        public static class DummySectionFragment extends Fragment {
            /**
             * The fragment argument representing the section number for this
             * fragment.
             */
            public static final String ARG_SECTION_NUMBER = "section_number";

            public DummySectionFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
                switch(getArguments().getInt(ARG_SECTION_NUMBER)){
                case 1:
                    //The constructor ListView_Adapter(MainActivity.DummySectionFragment) is undefined
                    listViewAdapter = new ListView_Adapter(this);
                    ListView listView = (ListView) rootView.findViewById(R.id.listView1);
                    for (int i=0;i<20;i++)
                    {
                        listViewAdapter.add("this Index : "+i);
                    }
                    return listView;
                }
                TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
                dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
                return rootView;
            }
        }

    }
like image 831
KeorynDeTar Avatar asked Aug 11 '13 22:08

KeorynDeTar


Video Answer


1 Answers

Instead of listViewAdapter = new ListView_Adapter(this);, you should instead try listViewAdapter = new ListView_Adapter(getActivity().getBaseContext());

The Issue is that you are passing a Fragment to the constructor, and not an Activity context.

like image 106
Shadesblade Avatar answered Sep 19 '22 12:09

Shadesblade