9

से कस्टम लिस्ट व्यू एडाप्टर को कॉल करना मैंने कुछ सूची लिखी है, मेरे लिए अवधारणा के सबूत के रूप में, मैं इसे कर सकता हूं। अब, मुझे एक टैब को एक टैब में एक टैब में गतिविधि को लोड करने में समस्या हो रही है, जिसमें कई टैब हैं, जो नेविगेशन के लिए स्वाइपिंग और टैब चयन दोनों की अनुमति देता है। मुझे त्रुटि मिलती है "जब मैं इसके लिए कोड लिखने की कोशिश करता हूं तो" कन्स्ट्रक्टर ListView_Adapter (MainActivity.DummySectionFragment) अपरिभाषित है "। मैं एक नौसिखिया हूं, और पिछले कुछ दिनों से मैंने यहां काफी कठिन परिश्रम किया है। किसी भी मदद की सराहना की है।फ्रैगमेंट क्लास मुख्य गतिविधि

टीएल; डीआर: मैं एक एन 00 बी हूं, और मैं इस समस्या को समझ नहीं सकता।

मेरा कस्टम सूची एडाप्टर

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

की कोशिश करनी चाहिए जारी करना है कि आप निर्माता के लिए एक टुकड़ा से गुजर रहे हैं है , और एक गतिविधि संदर्भ नहीं।

संबंधित मुद्दे