Question

I currently have 9 tabs which all display different items (using this tutorial http://www.androidbegin.com/tutorial/android-custom-listview-texts-and-images-tutorial/). my issue right now is that when I click on the item it doesn't go to my other activity which gives more detail on each item. I'm assuming it's because of the line return rootView at the end, yet I'm not sure what else to do in order to get display the items and the tabs. Here's my code:

public class CreatorFirstScreen extends FragmentActivity {

      SectionsPagerAdapter mSectionsPagerAdapter;

      ViewPager mViewPager;  

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

    // Create the adapter that will return a fragment for each of the three  
    // primary sections of the app.  
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());  

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

    PagerTabStrip strip = (PagerTabStrip)findViewById(R.id.tabstrip);
    strip.setTabIndicatorColor(0xFFFF);

}    

/** 
 * 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 9 total pages.  
        return 9;  
    }  

    @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);  
        case 3:  
            return getString(R.string.title_section4).toUpperCase(l); 
        case 4:  
            return getString(R.string.title_section5).toUpperCase(l); 
        case 5:  
            return getString(R.string.title_section6).toUpperCase(l); 
        case 6:  
            return getString(R.string.title_section7).toUpperCase(l); 
        case 7:  
            return getString(R.string.title_section8).toUpperCase(l); 
        case 8: 
            return getString(R.string.title_section9).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";  
    private ListView mainListView ;  
 // Declare Variables
    ListView list;
    ListViewAdapter adapter;
    String[] title;
    String[] comment;
    String[] rank;
    int[] picture;

    public DummySectionFragment() {  
    }  

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);  


        // Set the ArrayAdapter as the ListView's adapter.  
        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1:  
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,0002111131241352346262624t246gsdgw4vtwbbtqvtq3v5q3q5tq tq4tq4vrvq3v5q3vq2vq4bqv6w46w4tvw5w54b547be5bryene7bebev7we6q665ebeve", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 2:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "United States", "Indonesia"};  
                comment = new String[] {"315,761,000", "237,641,326"};
                picture = new int[] { R.drawable.unitedstates, R.drawable.indonesia };
                break;
            case 3:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 4:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 5:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 6:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 7:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 8:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
                break;
            case 9:
                // Generate sample data into string arrays
                rank = new String[] {"1","2"};
                title = new String[] { "China", "India" };
                comment = new String[] { "1,354,040,000", "1,210,193,422" };
                picture = new int[] { R.drawable.china, R.drawable.india }; 
        }

        // Locate the ListView in listview_main.xml
        mainListView = (ListView) rootView.findViewById(R.id.listView);

        // Pass results to ListViewAdapter Class
        adapter = new ListViewAdapter(getActivity(), rank, title, comment, picture);
        // Binds the Adapter to the ListView
        mainListView.setAdapter(adapter);
        // Capture ListView item click
        mainListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent i = new Intent(getActivity(), SingleItemView.class);
                // Pass all data rank
                i.putExtra("rank", rank);
                // Pass all data country
                i.putExtra("title", title);
                // Pass all data population
                i.putExtra("comment", comment);
                // Pass all data flag
                i.putExtra("picture", picture);
                // Pass a single position
                i.putExtra("position", position);
                // Open SingleItemView.java Activity
                startActivity(i);
            }
        });

        return rootView;  
    }  
}  

  } 
Was it helpful?

Solution

I believe the OnItemClickListener doesn't get attached to the mainListView because its root view hasn't yet been created. For it to work, try moving the related code (everything after the switch statement) to the onViewCreated method which fires right after onCreateView returns the rootView.

A CheckBox that's present in this ListView's row layout will steal its parent view's focus so the OnItemClickListener's onItemClick method won't get called. To overcome this, add android:focusable="false" and android:focusableInTouchMode="false" parameters to the CheckBox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top