我正在使用一個應用程序,其中我使用了選項卡/片段。我基於我在互聯網上找到的一個示例基於我的應用程序here。如何根據從一個片段發送到另一個片段的數據,使用片段來正確顯示我的Android佈局?
我所擁有的是在Home標籤中的ListView,它顯示了一個類的列表。我想要發生的事情是當我點擊列表中的一個類時,它會發送一個ID到Class選項卡,在那裏我想根據傳入的類ID填充該類中的學生的另一個ListView。我有問題讓這個工作正常。
目前,它主要工作。
這是我在課堂上標籤的佈局(即接收標籤):
class.xml
<LinearLayout android:id="@+id/class_information_layout">
<TextView android:id="@+id/class_id_display" />
<LinearLayout>
<TextView android:id="@+id/column1_row_header" />
<TextView android:id="@+id/column2_row_header" />
<TextView android:id="@+id/column3_row_header" />
<TextView android:id="@+id/column4_row_header" />
<TextView android:id="@+id/column5_row_header" />
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+swipe_container" />
<ListView android:id="@+student_row"></ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
這裏是填充該佈局類:
ClassInformation.java
package myPackage;
public class ClassInformation extends Fragment {
private View rootView;
private ListView myListView;
private SwipeRefreshLayout mySwipeRefresh;
private DatabaseHelper myDBHelper;
private Cursor informationCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private static String classID = "select a class first";
private TextView classIDDisplay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.class, container, false);
classIDDisplay = (TextView) rootView.findViewById(R.id.class_id_display);
myListView = (ListView) rootView.findViewById(R.id.student_row);
mySwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState){
super.onViewCreated(rootView, savedInstanceState);
if(getArguments() != null){
classID = getArguments().getString("classID");
}
System.out.println("Class ID: " + classID);
drawTheView();
mySwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh(){
mySwipeRefrehs.setRefreshing(false);
drawTheView();
}
});
}
private void drawTheView(){
myDBHelper = new DatabaseHelper(getActivity());
informationCursor = myDBHelper.getInformationCursor(classID);
String[] fromColumns = {"studentID", "firstname", "lastname", "homephone", "homeaddress"};
int[] toViews = {R.id.studentID_textview, R.id.firstname_textview, R.id.lastname_textview, R.id.homephone_textview, R.id.homeaddress_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.student_information, informationCursor, fromColumns, toViews, 0);
classIDDisplay.setText("Class ID: " + classID);
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
}
這是student_information佈局逐漸加入適配器:
student_information.xml
<LinearLayout>
<TextView android:id="@+id/column1_data" />
<TextView android:id="@+id/column2_data" />
<TextView android:id="@+id/column3_data" />
<TextView android:id="@+id/column4_data" />
<TextView android:id="@+id/column5_data" />
</LinearLayout>
這裏是將信息發送到類選項卡顯示在主頁選項卡類:
Home.java
package myPackage;
public class Home extends Fragment {
private View rootView;
private DatabaseHelper myDBHelper;
private Cursor classCursor;
private SimpleCursorAdapter mySimpleCursorAdapter;
private Activity myActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
ViewPager myViewPager = (ViewPager) getActivity.findViewById(R.id.pager);
myDBHelper = new DatabaseHelper(getActivity());
classCursor = myDBHelper.getClassCursor();
String[] fromColumns = {"classID"};
int[] toViews = {R.id.class_id_textview};
mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.class_layout, classCursor, fromColumns, toViews, 0);
ListView myListView = (ListView) rootView.findViewById(R.id.class_row);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position);
String classID = subCursor.getString(subCursor.getColumnIndex("classID"));
ClassInformation classInformation = new ClassInformation();
Bundle myBundle = new Bundle();
myBundle.putString("classID", classID);
classInformation.setArguments(myBundle);
getFragmentManager().beginTransaction().replace(R.id.class_information_layout, classInformation).commit();
myViewPager.setCurrentItem(1);
}
});
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
return rootView;
}
}
當前,當我第一次啓動應用程序並單擊主頁選項卡中的一個班級時,班級選項卡沒有從主頁選項卡中獲得「ID」,所以學生列表不會填充。然而,這是一個奇怪的部分,如果我(1)將我的Class選項卡關閉到另一個不是Home選項卡的選項卡並返回Class選項卡,或者(2)使用SwipeRefreshLayout向下滑動,學生將被正確繪製。
然後,如果我回到主頁選項卡並選擇不同的類,我的類選項卡仍顯示我選擇的第一個類。再次,如果我(1)單擊我的班級選項卡到另一個不是主頁選項卡並返回到班級選項卡,或(2)使用SwipeRefreshLayout向下滑動,學生列表將正確繪製與新班級的數據。
我沒有收到任何錯誤,只是直到我點擊標籤或刷卡刷新纔會正確顯示。
我在ClassInformation.java文件中有一個System.out.println。這是點擊左右我的應用程序的結果:
Start the app
System.out.println = "select a class first"
Click on a class in the Home tab
- System.out.println = "Class ID = <number>"
- nothing in the list
Click off of the Class tab, then back to the class tab -or- use the swipe refresh
- System.out.println = "Class ID = <number>"
- the list is populated correctly
Click a different class in the Home tab
- System.out.println = "<first class ID>"
- the list is not populated correctly
Click off of the Class tab, then back to the class tab -or- use the swipe refresh
- System.out.println = "Class ID = <new class number>"
- the list is populated correctly
- 或 -
Start the app
- System.out.println = "select a class first"
Click on a class in the Home tab
- System.out.println = "Class ID = <number>"
- nothing in the list
Swipe Refresh
- System.out.println = "Class ID = <number>"
- the list is populated correctly
Click a different class in the Home tab
- System.out.println = "<first class ID>"
- the list is not populated correctly
Swipe Refresh
- System.out.println = "Class ID = <new class number>"
- the list is populated correctly
我覺得這應該是一個簡單的修復,但我似乎保持俯瞰答案。
在Home片段爲什麼你在做另一個事務,當Class片段已經存在?您可以使用ClassInformation f =(ClassInformation)getSupportFragmentManager()來訪問Home片段。 f.setClassId(); 。在上面的代碼中,用ViewPager中ClassInformation的實際位置替換1。方法setClassId()(在ClassInformation中)看起來像這樣public void setClassId(String id){classID = id; if(getView()!= null){drawTheView();}}。 – Luksprog
當我嘗試這個,我得到一個空指針異常,因爲我的「ClassInformation f」爲空。雖然我使用findFragmentByTag時返回String。 – Brian
您使用ViewPager的適配器類型是什麼?您是否根據ViewPager中ClasInformation的位置使用了正確的位置(位置從0開始)? – Luksprog