9

私はそれを行うことができる自分自身の概念証明として、いくつかのlistViewアクティビティを作成しました。今では、複数のタブを持つアプリの1つのタブにlistViewアクティビティを読み込むのに問題があります。これは、スワイプとタブの選択の両方を可能にします。そのコードを記述しようとすると、「コンストラクタListView_Adapter(MainActivity.DummySectionFragment)が未定義です」というエラーが表示されます。私は初心者です。ここ数日、私はかなり熱心にここに潜んでいます。どんな助けもありがとうございます。フラグメントクラスメインアクティビティからカスタムListViewアダプタを呼び出す

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

    } 

答えて

19

代わりのlistViewAdapter = new ListView_Adapter(this);、あなたの代わりにlistViewAdapter = new ListView_Adapter(getActivity().getBaseContext());

を試みるべき問題は、コンストラクタにフラグメントを渡しているということです、アクティビティコンテキストではありません。

関連する問題