这是我的 ListFragment 代码。它创建 simpleCursorAdaptor,然后使用它来填充列表视图。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Anuranjit", LaunchActivity.selectedDate);
SQLiteDatabase allDatabase2 = new TaskDatabaseManager(getActivity()).getWritableDatabase();
Cursor allDetail = allDatabase2.rawQuery("Select "+ TaskDatabaseManager.KEY_ROWID + ","+ TaskDatabaseManager.TASK_TIMES + ","+ TaskDatabaseManager.TASK_DESCRIPTION + " from "+ TaskDatabaseManager.DATABASE_TABLE + " where "+ TaskDatabaseManager.TASK_DATE + "= '" + LaunchActivity.selectedDate + "'", null);
String[] fromColumns = { TaskDatabaseManager.TASK_TIMES,
TaskDatabaseManager.TASK_DESCRIPTION };
int[] toView = { R.id.Time, R.id.TaskDescription };
allDetail.moveToFirst();
setListAdapter(new SimpleCursorAdapter(getActivity(),R.layout.layout_for_cursor, allDetail, fromColumns, toView, 0));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
这是 ListFragment 类。现在我的 activity_main.xml 如下
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Main_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/Date_List"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
现在问题是我没有在 int ListFragment 中获取任何数据。提交 ListFragment 的代码在主要活动的 onCreate() 内
Fragment taskFragment = new TaskFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.MainContentLayout, taskFragment);
fragmentTransaction.commit();
和 fragment_layout.xml 是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainContentLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</LinearLayout>
我刚刚开始学习android。