0

告诉我我做错了什么?

我正在尝试填写表格。但是数据没有出现在 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>
4

3 回答 3

4

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

替换您的以下代码:

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

有了这个:

return view;
于 2013-01-02T12:01:45.833 回答
2

您膨胀另一个 LinearLayout 并返回它。所以你得到空屏幕使用这个方法而不是上面

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;
}
于 2013-01-02T12:03:56.573 回答
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">
...
于 2014-03-24T12:02:41.030 回答