以下ListFragment不顯示除空白屏幕之外的任何內容。我知道數據存在。我沒有收到錯誤,只是一個空白(白色)屏幕。我相信這是愚蠢的。我對Android相當陌生。我的Android ListFragment不顯示列表
package com.pbs.deliverytrack1;
import com.pbs.deliverytrack1.DBContract;
import com.pbs.deliverytrack1.DBHelper;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OrderListFragment extends ListFragment {
SimpleCursorAdapter mAdapter;
String TAG = "OrderListFragment";
static final String[] PROJECTION = new String[] {
DBContract.DeliveryOrderTable._ID,
DBContract.DeliveryOrderTable.CUSTOMER,
DBContract.DeliveryOrderTable.ADDRESS };
// Selection criteria
static final String SELECTION = "(("
+ DBContract.DeliveryOrderTable.CUSTOMER + " NOTNULL) AND ("
+ DBContract.DeliveryOrderTable.CUSTOMER + " != '') AND ("
+ DBContract.DeliveryOrderTable.DELIVERED_DATETIME + " = ''))";
private OnOrderSelectedListener listener = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String tag = TAG + ".onCreate()";
Log.d(tag,"Fragment created");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String tag = TAG + ".onCreateView()";
Log.d(tag, "Inflating fragment_order_list - or trying to.");
View view = inflater.inflate(R.layout.fragment_order_list, container,
false);
if (view == null) {
Log.d(tag, "Problem inflating view, returned null");
}
initializeList();
return view;
}
public void onActivityCreated(Bundle bundle) {
super.onActivityCreated(bundle);
String tag = TAG + ".onActivityCreated()";
Log.d(tag,"Parent Activity Created");
}
public interface OnOrderSelectedListener {
public void onOrderSelected(long orderId);
// show detail record you dummy
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
String tag = "OrderListFragment.onAttach()";
Log.d(tag,"Attached!");
if (activity instanceof OnOrderSelectedListener) {
listener = (OnOrderSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implement MyListFragment.OnItemSelectedListener");
}
} // onAttach()
public void onStart() {
super.onStart();
Log.d(TAG + ".onStart()", "Started!!!");
}
public void onResume() {
super.onResume();
Log.d(TAG + "onResume()", "Resumed!!!");
}
public void onPause() {
super.onPause();
Log.d(TAG + ".onPause()", "Paused");
}
public void onStop() {
super.onStop();
Log.d(TAG + ".onStop()", "Stopped");
}
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG + ".onDestroyView()", "View Destroyed");
}
public void onDestroy() {
super.onDestroy();
Log.d(TAG + ".onDestroy()", "I'm dying!!!!");
}
public void onDetach() {
super.onDetach();
Log.d(TAG + ".onDetach()", "Off with the fragment!");
}
private void initializeList() {
String tag = TAG + ".initilizeList()";
Log.d(tag,"Setting up cursor.");
// for the cursor adapter, specify which columns go into which views
String[] fromColumns = {
DBContract.DeliveryOrderTable._ID,
DBContract.DeliveryOrderTable.CUSTOMER,
DBContract.DeliveryOrderTable.ADDRESS };
int[] toViews = {
R.id.list_view_order_id,
R.id.list_view_customer_field,
R.id.list_view_address_field };
// create an empty adapter we will use to display the loaded data.
DBHelper dbHelper = new DBHelper(getActivity());
Cursor cursor = dbHelper.getAllOrdersCursor();
if (cursor != null) {
cursor.moveToFirst();
Log.d(tag,"Creating SimpleCursorAdapter mAdapter");
mAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.fragment_order_list_row,
cursor,
fromColumns,
toViews,
0);
setListAdapter(mAdapter);
int count = mAdapter.getCount();
Log.d(tag, "Order Record Count = " + count);
if (mAdapter.isEmpty()) {
Log.d(tag, "Alas! mAdapter is empty");
} // end if mAdapter empty
} else { // cursor is null
Log.d(tag,"The cursor is null");
} // if cursor != null
}
}
該片段應該顯示從SQLite查詢派生的列表。我正在構建的用戶界面是平板電腦上的簡單拆分列表/詳細信息屏幕。我正在爲Honeycomb構建,因此我正在使用支持庫。
讓我知道你是否需要查看代碼的其他部分。
這裏的fragment_order_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#00FF00"
android:drawSelectorOnTop="false" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="@string/err_no_data" />
</LinearLayout>
這裏的活動ListFragment從所謂:
package com.pbs.deliverytrack1;
import com.pbs.deliverytrack1.OrderListFragment.OnOrderSelectedListener;
import com.pbs.deliverytrack1.DBHelper;
import com.pbs.deliverytrack1.DeliveryOrder;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends FragmentActivity implements
OnOrderSelectedListener {
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String tag = TAG + ".onCreate()";
Log.d(tag, "Setting up database.");
new SetupDatabase().execute(this);
Log.d(tag, "Setting Content View");
setContentView(R.layout.activity_main);
}
@Override
public void onOrderSelected(long orderId) {
String tag = TAG + ".onOrderSelected()";
Log.d(tag,"Creating order detail fragment");
OrderDetailFragment fragment = (OrderDetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.detailFragment);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
這裏的activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="@+id/action_bar_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.pbs.deliverytrack1.MyActionBarFragment"
tools:layout="@layout/fragment_actionbar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
>
<fragment
android:id="@+id/listFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="40"
android:layout_gravity="bottom"
class="com.pbs.deliverytrack1.OrderListFragment" />
<fragment
android:id="@+id/detailFragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="60"
class="com.pbs.deliverytrack1.OrderDetailFragment"
tools:layout="@layout/fragment_order_detail" />
</LinearLayout>
</LinearLayout>
要明確,您會在Log中看到''Order Record Count = xx''?出於好奇,計數是多少?如果你要膨脹自己的佈局,你也不應該調用'super.onCreateView()'。請發佈'fragment_order_list.xml'。 – Sam 2013-04-11 22:15:16
有41條記錄,是的,這就是顯示的計數。 :) – Rben 2013-04-12 00:02:28
剛剛添加了fragment_order_list.xml – Rben 2013-04-12 00:05:03