Java 为活动片段中的ListView设置ListView适配器
我需要设置ListView的适配器,它位于活动的片段中。 我试图用下面的代码从活动中设置适配器,但它返回一个Java 为活动片段中的ListView设置ListView适配器,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,我需要设置ListView的适配器,它位于活动的片段中。 我试图用下面的代码从活动中设置适配器,但它返回一个NullPointerException,可能是因为片段尚未加载 ListView gradeListView=(ListView)this.findViewById(R.id.gradeListView); setAdapter(新的SimpleAdapter(此,项列表,列表布局,列表输入,布局值)); 是否有一种方法可以在加载片段后运行上述代码,或者有一种更好的方法可以这样做?我尝
NullPointerException
,可能是因为片段尚未加载
ListView gradeListView=(ListView)this.findViewById(R.id.gradeListView);
setAdapter(新的SimpleAdapter(此,项列表,列表布局,列表输入,布局值));
是否有一种方法可以在加载片段后运行上述代码,或者有一种更好的方法可以这样做?我尝试使用一种方法,在片段本身内部设置适配器,但我仍然需要等待片段加载,然后再从活动中启动它,否则我仍然会收到NullPointerException
。我还需要将一个ArrayList
放入一个bundle中,如果我要使用该方法,没有关于如何这样做的文档
我还尝试从片段的onActivityCreated()
方法中创建一个方法,该方法保存位于活动内部的set适配器代码,该方法仍然返回NullPointerException
我是否没有从活动中正确引用ListView元素,或者是否有方法等待片段加载,这样我就不会收到错误
TabbedActivity.java/主父活动
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_选项卡);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//活动的主要部分。
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.container);
mViewPager.setOffscreenPageLimit(6);
mViewPager.setAdapter(mSectionsPagerAdapter);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
TabLayout TabLayout=(TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mviewpage);
// ...
解释等级(retrieveGrades());
}
公共等级(字符串输入){
//上课~~mainClassedivider~~GRADES
//把它变成类
//等级=等级
最后一个字符串[]ServerResponse=input.split(“~~mainclasseparator~~”;
最后一个字符串[]Classes=ServerResponse[0]。拆分(;);
最后一个字符串[]Grades=ServerResponse[1]。拆分(“;”);
ArrayList item_list=新建ArrayList();
for(int i=0;isubstring(1,--.length())从字符串中删除第一个字符(在本例中,从类名中删除一个空格)
item.put(“Class”,Class.substring(1,Class.length());
项目.投入(“等级”,等级[i]);
项目列表。添加(项目);
}
字符串[]列表\输入=新字符串[]{“期间”、“类别”、“等级”};
//将列表_输入的字符串值放入XML布局值中
int[]layout_values=newint[]{android.R.id.text1,android.R.id.text2,R.id.grade};
//LISTVIEW布局(XML文件)
int list_LAYOU=等级_列表;
ListView gradeListView=(ListView)this.findViewById(R.id.gradeListView);
setAdapter(新的SimpleAdapter(此,项列表,列表布局,列表输入,布局值));
}
Tab2Pulse.java/Fragment
public class Tab2Pulse extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.tab2chat, container, false);
return mainView;
}
}
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="software.endeavor.projectg.TabbedActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/main_gradient"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<View
android:layout_width="match_parent"
android:layout_height="12dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/main_gradient"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<View
android:id="@+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_below="@id/toolbar"
android:background="@drawable/toolbar_dropshadow" />
<!--<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="368dp"
android:layout_height="495dp">-->
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@*android:dimen/navigation_bar_height"
android:background="@drawable/login_gradient" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
tab2chat.xml/Fragment布局
public class Tab2Pulse extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.tab2chat, container, false);
return mainView;
}
}
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="software.endeavor.projectg.TabbedActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/main_gradient"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<View
android:layout_width="match_parent"
android:layout_height="12dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/main_gradient"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<View
android:id="@+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_below="@id/toolbar"
android:background="@drawable/toolbar_dropshadow" />
<!--<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="368dp"
android:layout_height="495dp">-->
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@*android:dimen/navigation_bar_height"
android:background="@drawable/login_gradient" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
活动\u tabbed.xml/主要活动布局
public class Tab2Pulse extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.tab2chat, container, false);
return mainView;
}
}
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="software.endeavor.projectg.TabbedActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/main_gradient"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<View
android:layout_width="match_parent"
android:layout_height="12dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/main_gradient"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<View
android:id="@+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_below="@id/toolbar"
android:background="@drawable/toolbar_dropshadow" />
<!--<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="368dp"
android:layout_height="495dp">-->
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@*android:dimen/navigation_bar_height"
android:background="@drawable/login_gradient" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
如果您的listview位于片段上,并且您的数据位于活动上,并且您需要用活动的数据填充listview,那么您可以在活动上创建一个公共字段,然后通过(“您的活动名称”)getActivity()访问它们.field_name
@alisamawi即使我从片段本身设置它,我也会得到一个NullPointerException。你能把你的代码放在这里自己检查一下吗。@alisamawi我已经将它添加到帖子中。在片段类(onViewCreated
或onActivityCreated
)中初始化你的片段,而不是在活动类中。您可以通过接口或Tab2Pulse
类本身从活动传递所需的数据。