ListFragment,不显示数据

时间:2013-12-18 16:32:50

标签: java android xml

我试图让我的ListFragment显示在我的视图中,但是我对这段代码有无穷无尽的问题而无法让它显示我的解析器的结果。我相信它最有可能是我的xml文件的问题,我在代码中没有收到任何错误,它只是不显示ListView。

activity_main.xml中

}

    <RelativeLayout 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" >


    <ListView
        android:id="@+id/list"
        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"
        class="com.example.CourseList">

    </ListView>
</RelativeLayout>

list_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/course"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="36sp" />

    <TextView
        android:id="@+id/room"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/course"
        android:layout_alignBottom="@+id/course"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/course"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/building"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/room"
        android:layout_alignBottom="@+id/room"
        android:layout_toRightOf="@+id/room"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

我相信您需要致电onViewCreated。尝试更改此行代码:

     @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        int num = getArguments().getInt(ARG_SECTION_NUMBER);
        records = dao.getAllRecords();
        mAdapter = new TimetableAdapter(getActivity(), android.R.id.list, records);
        listView.setAdapter(mAdapter);
    }

对此:

   @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int num = getArguments().getInt(ARG_SECTION_NUMBER);
        records = dao.getAllRecords();
        mAdapter = new TimetableAdapter(getActivity(), android.R.id.list, records);
        listView.setAdapter(mAdapter);
    }

答案 1 :(得分:0)

首先更改ID,如果是ListView。

<ListView android:id="@android:id/list" >
</ListView>

在Fragment中,

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_main,
                container, false);
        listView = (ListView) rootView.findViewById(android.R.id.list);

        return rootView;
    }

这应该是你的活动布局(R.layout.activity_layout)

<FrameLayout 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:id="@+id/frameLayout"
    tools:context=".MainActivity" >

</FrameLayout>

在你的活动中,

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);
    getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new YourFragment()).commit();
}

您可以像

一样编写适配器
class TimetableAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Timetable> records;
            private final LayoutInflater inflater;

    public TimetableAdapter(Context context, List<Timetable> records) {
        this.context = context;
        this.records = new ArrayList<Timetable>(records);
                    infalter  = LayoutInflater.from(context);
    }

    private static class ViewHolder {
        TextView course;
        TextView room;
        TextView building;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Timetable rowItem = getItem(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.courselist_layout, null);
            holder = new ViewHolder();
            holder.course = (TextView) convertView.findViewById(R.id.course);
            holder.room = (TextView) convertView.findViewById(R.id.room);
            holder.building = (TextView) convertView
                    .findViewById(R.id.building);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.course.setText(rowItem.getCourse());
        holder.room.setText(rowItem.getRoom());
        holder.building.setText(rowItem.getBuilding());

        return convertView;

    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int position) {
        return records.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

并写

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    int num = getArguments().getInt(ARG_SECTION_NUMBER);
    records = dao.getAllRecords();
    mAdapter = new TimetableAdapter(getActivity(),  records);
    listView.setAdapter(mAdapter);
}