0

以下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> 
+0

要明确,您会在Log中看到''Order Record Count = xx''?出于好奇,计数是多少?如果你要膨胀自己的布局,你也不应该调用'super.onCreateView()'。请发布'fragment_order_list.xml'。 – Sam 2013-04-11 22:15:16

+0

有41条记录,是的,这就是显示的计数。 :) – Rben 2013-04-12 00:02:28

+0

刚刚添加了fragment_order_list.xml – Rben 2013-04-12 00:05:03

回答

0

MainActivity的的LinearLayout设置为:

android:orientation="horizontal" 

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

+1

谢谢,山姆,你是一个学者和绅士。 – Rben 2013-04-12 16:56:41