0

以下 ListFragment 除了空白屏幕外不显示任何内容。我知道数据是存在的。我没有收到错误,只是一个空白(白色)屏幕。我敢肯定这很傻。我对Android相当陌生。

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 查询派生的列表。我正在构建的 UI 是平板电脑上的简单拆分列表/详细信息屏幕。我正在为 pre-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>
4

1 回答 1

0

MainActivity 的 LinearLayout 设置为:

android:orientation="horizontal" 

但是您似乎有一个"vertical"设计,因为您的第一个孩子的宽度是"match_parent". 这意味着 ListView 被绘制,就在屏幕的右侧......只需更改根布局的方向。

于 2013-04-12T15:13:46.147 回答