9

我写了一些 listView 活动,作为我自己的概念证明,我可以做到。现在,我无法将 listView 活动加载到具有多个选项卡的应用程序的单个选项卡中,该应用程序允许滑动和选项卡选择以进行导航。当我尝试为其编写代码时,我收到错误“构造函数 ListView_Adapter(MainActivity.DummySectionFragment) 未定义”。我是初学者,过去几天我一直潜伏在这里。任何帮助表示赞赏。

TL;DR : 我是一个 n00b,我无法解决这个问题。

我的自定义列表适配器

    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);
            }

        }
    }

我的主要活动

    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;
            }
        }

    }
4

1 回答 1

19

而不是listViewAdapter = new ListView_Adapter(this);,您应该尝试listViewAdapter = new ListView_Adapter(getActivity().getBaseContext());

问题是您将 Fragment 传递给构造函数,而不是 Activity 上下文。

于 2013-08-11T23:45:50.157 回答