1

我使用 SimpleCursorAdapter 的子类作为适配器扩展了支持库中的 ListFragment。所有数据加载部分都可以(通过调试读取),列表没有显示,我在 Activity 上有一个空白页面。这是我的代码:

import android.support.v4.app.FragmentActivity;

public class HostActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_with_fragment);
}
}

布局文件(activity_view_with_fragment.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <fragment
            android:id="@+id/lsStopTimetableFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:layout_weight="1"
            class="com.package.Fragment.MyFragment">
    </fragment>
</LinearLayout>

这是 ListFragment 代码:

import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
public class MyFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
    public  void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        String [] from = new String[] {"col_1", "col_2"}; 
        int [] to = new int [] {R.id.view1, R.id.view2}; 
        //..put value in args
        Loader l = getLoaderManager().restartLoader(0, args, this);
        if(l != null)
            l.forceLoad();

        adapter = new CustomCursorAdapter(this.getActivity()
              , R.layout.row_entry, null, from,
                to, 0);
        setListAdapter(adapter);
    }
    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        adapter.swapCursor(cursor);
    }
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        adapter.swapCursor(null);
    }

}

在 CustomCursorAdapter 中,我确实实现了

getView (int pos, View convertView, ViewGroup parent)

方法。任何提示?提前致谢。

4

1 回答 1

1

您是否尝试将片段项目中的 layout_width 和 layout_height 参数从 0dp 更改为 match_parent?如果您尝试创建一个仅包含填充整个活动的列表片段的活动,则无需创建如下布局:

<LinearLayout>
<fragment/>
</LinearLayout>

您可以直接使用片段标签作为根标签和唯一标签创建布局:

<fragment/>
于 2012-12-10T20:25:24.893 回答