好吧,我试图在片段中找到一个 ListView,以便能够通过创建它的 Main Activity 添加和删除项目。这是我的尝试(我基于 com.viewpagerindicator.sample),但是当我最终尝试访问 mainListView 时,它崩溃了。
public final class ContactListFragment extends Fragment {
private ListView MyListView;
public ListView GetListView()
{
return MyListView;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
LinearLayout theLayout = (LinearLayout) inflater.inflate(R.layout.tab_frag, container, false);
MyListView = (ListView)theLayout.findViewById(R.id.listViewx);
MyListView.setAdapter(adapter);
return theLayout;
}
}
这里是布局
<ListView
android:id="@+id/listViewx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
这就是我想要访问 listview 以便能够修改公共类 ContactListActivity 的 Main Activity 的代码 extends BaseSampleActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, ContactListFragment.class.getName()));
fragments.add(Fragment.instantiate(this, ContactListFragment.class.getName()));
fragments.add(Fragment.instantiate(this, ContactListFragment.class.getName()));
PagerAdapter mAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
TabPageIndicator mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
ContactListFragment XCLF = (ContactListFragment) fragments.get(1);
ListView mainListView = (ListView) XCLF.GetListView();
}
任何帮助将不胜感激,谢谢