Android - Fragment中的ListView不显示

时间:2013-01-02 11:57:11

标签: android android-layout android-fragments

告诉我我做错了什么?

我正在尝试填写表格。 但是数据不会出现在ListView中。

例如,在Activity中它正在运行。 这段代码没有尝试过,但是从它完美运行的地方复制过来。

数据准确,检查调试器。

public class TabFilter extends Fragment {

    private static String TAB_FILTER_LOG = "TabFilter: ";
    private ArrayList<HashMap<String, Object>> filterListData;
    private ListView filterListView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);

        View view = inflater.inflate(R.layout.tab_filter, container, false);

        filterListView = (ListView) view.findViewById(R.id.filtersListView);
        filterListData = new ArrayList<HashMap<String, Object>>();

        fillListView();

        SimpleAdapter filtersListAdapter = new SimpleAdapter(getActivity().getBaseContext(), filterListData, R.layout.filters_list_view_row, new String[] { "name",
                "nummer" }, new int[] { R.id.filterNameTv, R.id.filterInfoTv });

        filterListView.setAdapter(filtersListAdapter);
        filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        // filterListView.setOnItemClickListener(this);

        if (container == null) {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.tab_filter, container, false);
    }

    private HashMap<String, Object> fillListView() {
        HashMap<String, Object> fillMap = null;

        SQLiteDatabase db = DatabaseHelper.getInstance(getActivity().getBaseContext()).getWritableDatabase();
        Cursor cursor = db.query("filters", null, null, null, null, null, null);

        if (cursor.moveToFirst()) {

            int nameColIndex = cursor.getColumnIndex("name");
            int nummerColIndex = cursor.getColumnIndex("nummer");

            do {
                fillMap = new HashMap<String, Object>();

                fillMap.put("name", cursor.getString(nameColIndex));
                fillMap.put("nummer", cursor.getString(nummerColIndex));
                filterListData.add(fillMap);

            } while (cursor.moveToNext());

            cursor.close();
            db.close();
        } else {
            Toast.makeText(getActivity().getBaseContext(), "0 ROWS: ", Toast.LENGTH_LONG).show();
        }
        return fillMap;
    }
}

tab_filter.xm

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

    <ListView
        android:id="@+id/filtersListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

filters_list_view_row.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" 
    android:background="@color/BlueViolet">

    <TextView
        android:id="@+id/filterNameTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:gravity="center"
        android:background="@color/LightGrey"/>

    <TextView
        android:id="@+id/filterInfoTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/LightSkyBlue" />

</LinearLayout>

3 个答案:

答案 0 :(得分:4)

您需要在onCreateView()中返回视图对象。请检查onCreateView(),在第一行中创建View对象,并创建与第一个视图对象相关的listview对象。最后,您要膨胀另一个LinearLayout并返回它。所以你得到空屏幕。

替换以下代码:

if(container == null) {
    return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab_filter, container, false);

用这个:

return view;

答案 1 :(得分:2)

您为另一个LinearLayout充气并返回它。所以你得到空屏幕 使用此方法代替Above

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.tab_filter, container, false);

    filterListView = (ListView) view.findViewById(R.id.filtersListView);
    filterListData = new ArrayList<HashMap<String, Object>>();

    fillListView();

    SimpleAdapter filtersListAdapter = new SimpleAdapter(getActivity().getBaseContext(), filterListData, R.layout.filters_list_view_row, new String[] { "name",
                 "nummer" }, new int[] { R.id.filterNameTv, R.id.filterInfoTv });

    filterListView.setAdapter(filtersListAdapter);
    filterListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // filterListView.setOnItemClickListener(this);

    if(container == null) {
        return null;
    }
    return view;
}

答案 2 :(得分:0)

我有相同的片段问题没有出现。 问题是XML语句的顺序,因此方向应该出现在“布局”之前:

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

    android:background="@color/BlueViolet">
...