我需要设置一个ListView的Adapter,它位于Activity的Fragment中。
我尝试使用下面的代码从Activity中设置适配器,但它返回NullPointerException
,可能是因为片段尚未加载。
ListView gradeListView = (ListView) this.findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));
有没有办法在Fragment加载后运行上面的代码,或者更好的方法完全执行此操作?我试图使用一种方法将Frag内部设置为适配器,但是我仍然需要等待片段加载才能从Activity中发起它,否则我仍然会收到NullPointerException
。我还需要将ArrayList<HashMap<String, String>>
放入一个包中,如果我打算使用该方法,则没有关于如何操作的文档。
我还试图启动一个方法,该方法保存位于活动内部的设置适配器代码,该代码仍位于片段的onActivityCreated()
方法中,该方法仍返回NullPointerException
。
我没有正确地从Activity中引用ListView元素,还是有办法等待Fragment加载,所以我没有收到错误?
TabbedActivity.java /主要父活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setOffscreenPageLimit(6);
mViewPager.setAdapter(mSectionsPagerAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// ...
interpretGrades(retrieveGrades());
}
public void interpretGrades(String input) {
// take CLASSES~~MAINCLASSDIVIDER~~GRADES
// turn it into Classes = Classes
// Grades = Grades
final String[] ServerResponse = input.split("~~MAINCLASSDIVIDER~~");
final String[] Classes = ServerResponse[0].split(";");
final String[] Grades = ServerResponse[1].split(";");
ArrayList<HashMap<String, String>> item_list = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Classes.length; i++) {
HashMap<String, String> item = new HashMap<String, String>();
String[] ClassSplit = Classes[i].split("-");
String Period = ClassSplit[0];
String Class = ClassSplit[1];
item.put("Period", Period);
// --.substring(1, --.length()) removes first character from string (in this case, a whitespace from the class name)
item.put("Class", Class.substring(1, Class.length()));
item.put("Grade", Grades[i]);
item_list.add(item);
}
String[] list_input = new String[] { "Period", "Class", "Grade" };
// PUT THE STRING VALUES FROM list_input INTO THE XML LAYOUT VALUES
int[] layout_values = new int[] { android.R.id.text1, android.R.id.text2, R.id.grade};
// LISTVIEW LAYOUT (XML FILE)
int list_layout = grade_list;
ListView gradeListView = (ListView) this.findViewById(R.id.gradeListView);
gradeListView.setAdapter(new SimpleAdapter(this, item_list, list_layout, list_input, layout_values));
}
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;
}
}
tab2chat.xml /片段布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tab2chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/gradeListView"
style="@android:style/Widget.Material.Light.ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
activity_tabbed.xml /主要活动布局
<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>