我目前有9个标签,都显示不同的项目(使用本教程http://www.androidbegin.com/tutorial/android-custom-listview-texts-and-images-tutorial/)。我现在的问题是,当我点击该项目时,它不会转到我的其他活动,它会提供有关每个项目的更多详细信息。我假设它是因为最后的行return rootView
,但我不知道还有什么可以做以显示项目和标签。这是我的代码:
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;
}
}
}
答案 0 :(得分:1)
我认为 OnItemClickListener
没有附加到mainListView
,因为它的根视图尚未创建。要使其工作,请尝试将相关代码(switch语句后的所有内容)移动到onViewCreated
方法,该方法在onCreateView
返回rootView
后立即触发。
此CheckBox
行布局中存在的ListView
将窃取其父视图的焦点,因此不会调用OnItemClickListener
的{{1}}方法。要解决此问题,请添加onItemClick
以及android:focusable="false"
的{{1}}参数。