向导里的Swipe Views + Title Strip,在fragment里添加listview,怎么不显示
利用eclipse的创建向导,选择了Swipe Views + Title Strip,然后计划在fragment里添加个listview,建了Adpater,但数据就是显示不出来。代码如下:public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_english_home);
// 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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
/**
* 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) {
switch (position) {
case 0:
Fragment fragment = new MessageSectionFragment();
Bundle args = new Bundle();
args.putInt(MessageSectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
case 1:
Fragment fragment1 = new MyFriendSectionFragment();
Bundle args1 = new Bundle();
args1.putInt(MyFriendSectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment1.setArguments(args1);
return fragment1;
case 2:
Fragment fragment2 = new SetupSectionFragment();
Bundle args2 = new Bundle();
args2.putInt(SetupSectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment2.setArguments(args2);
return fragment2;
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "练习";
case 1:
return "课程";
case 2:
return "";
}
return null;
}
}
public static class MessageSectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
ArrayList<homeitemModel> itemList = null;
ListView listview = null;
listviewAdapter myAdapter = null;
public MessageSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home_message, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
itemList = new ArrayList<homeitemModel>();
homeitemModel item1 = new homeitemModel();
item1.setCaption("test1");
itemList.add(item1);
homeitemModel item3 = new homeitemModel();
item3.setCaption("test2");
itemList.add(item3);
homeitemModel item2 = new homeitemModel();
item2.setCaption("test3");
itemList.add(item2);
View listMsg = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_home_message, null);
listview = (ListView)listMsg.findViewById(R.id.english_home_message_listview);
myAdapter = new listviewAdapter(listMsg.getContext());
listview.setAdapter(myAdapter);
}
public class listviewAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public listviewAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_homeactivity, null);
holder = new ViewHolder();
holder.image = (ImageView)convertView.findViewById(R.id.imageView_homeactivity);
holder.caption = (TextView)convertView.findViewById(R.id.textView_caption_homeactivity);
holder.unread = (TextView)convertView.findViewById(R.id.textView_count_homeactivity);
holder.description = (TextView)convertView.findViewById(R.id.textView_description_homeactivity);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
holder.image.setImageBitmap(null);
holder.caption.setText(null);
holder.unread.setText(null);
holder.description.setText(null);
}
holder.caption.setText(itemList.get(position).getCaption());
System.out.println(itemList.get(position).getCaption());
return convertView;
}
}
public final class ViewHolder {
public ImageView image;
public TextView unread;
public TextView caption;
public TextView description;
}
}
public static class MyFriendSectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public MyFriendSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home_setup, container, false);
}
}
public static class SetupSectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public SetupSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home_setup, container, false);
}
}
}
折腾一下午了,没找出来到底问题在哪儿? --------------------编程问答-------------------- 没人跟贴,自己又折腾了一个早上,终于搞定了。分享出来
关键的问题是Fragment里的onCreateView和onCreate。
把以下的代码放在onCreateView里就OK了
View view = inflater.inflate(R.layout.fragment_home_message, container, false);
listview = (ListView)view.findViewById(R.id.fragment_english_home_message_listview);
myAdapter = new listviewAdapter(view.getContext());
listview.setAdapter(myAdapter);
return view --------------------编程问答-------------------- 楼主在吗?我想请教下,关于Swipe Views + Title Strip --------------------编程问答-------------------- 我也遇到这样的问题,请问你的homeitemModel从哪里来的?谢谢 --------------------编程问答-------------------- 如果是碎片里的listView,在主活动的onCreate里面setAdapter的话会有NullPointerException,貌似我试了几次都是这样 --------------------编程问答--------------------
是的,我也遇到这个问题
补充:移动开发 , Android