这是我的ListFragment Code.Which创建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 Class。现在我的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.Code中获取任何数据,因为提交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。
答案 0 :(得分:0)
光标为空。原因可能是日期比较可能导致问题。尝试使用YourDatabase.query()方法查询数据库。 见这里:http://developer.android.com/training/basics/data-storage/databases.html