1

I'm currently using a ListFragment together with an ExpandableListView to show some data backed by a SimpleCursorTreeAdapter. Everything works fine, but I recently switched to the support.v4 package, to make use of the ViewPager class to swipe between tabs. Swiping and all the other classes that now use the support.v4.Fragment work fine, but my ListFragment has stopped working.

There are no exceptions thrown, but the ListFragment simply doesn't show any items.

This is the code for the ListFragment:

public class VisuTextFragment extends ListFragment {

    private Storage mStorage;
    private int mFilterSensortype;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStorage = Storage.newSQLiteDatabase(getActivity());
        mFilterSensortype = -1;
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        return inflater.inflate(R.layout.l_visu_text, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fillData();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.textvis, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_filter:
            final Dialog dialog = new Dialog(getActivity());
            dialog.setTitle("Filter by sensor type");
            dialog.setContentView(R.layout.l_dialog_filter);

            Button ok = (Button) dialog.findViewById(R.id.filter_ok);
            ok.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                int filter = Integer.parseInt(((EditText) dialog.findViewById(R.id.et_filter)).getText().toString());
                    mFilterSensortype = filter;
                    fillData();
                    dialog.dismiss();
                }
            });

            Button cancel = (Button) dialog.findViewById(R.id.filter_cancel);
            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            dialog.show();
            break;
        }
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        fillData();
    }

    public void fillData() {
        Log.d("VisuTextFragment", "fillData()");
        Cursor cursor;
        if (mFilterSensortype == -1)
            cursor = mStorage.queryAllAsCursor();
        else
            cursor = mStorage.query(mFilterSensortype);

        TextVisCursorAdapter adapter = new TextVisCursorAdapter(
                getActivity(),
                cursor,
                R.layout.l_visu_text_group,
                new String[] { Storage.ELEMENT_ID, Storage.ELEMENT_ENTRIES_DATE, Storage.ELEMENT_ENTRIES_LATITUDE, Storage.ELEMENT_ENTRIES_LONGITUDE, Storage.ELEMENT_ENTRIES_SENSORTYPE },
                new int[] { R.id.id, R.id.date, R.id.latitude, R.id.longitude, R.id.sensortype },
                R.layout.l_visu_text_child,
                new String[] { Storage.ELEMENT_MEASUREMENTS_VALUE },
                new int[] { R.id.value });

        ListView lv = (ListView) getListView();
        ExpandableListView elv = (ExpandableListView) lv;
        elv.setGroupIndicator(null);
        elv.setAdapter(adapter);
    }
}

And this is the layout that I'm using (don't know if that helps):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/id"/>
    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="@string/date"/>
    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="@string/latitude"/>
    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="@string/longitude"/>
    <TextView
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/sensortype"/>
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ExpandableListView android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:transcriptMode="normal"/>
    <TextView android:id="@+id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/no_entries"/>

</LinearLayout>
</LinearLayout>

Just FYI: The TextView's in the layout file are shown, but the list itself is just missing. Not even the TextView for an empty list is shown.

Hope you can help.

EDIT: I have checked for the ExpandableListView's width and height via their corresponding methods and both return a value of 0. Its getCount() method returns 347. So the View definitely exists and is filled properly, but it is for some weird reason just not drawn to the screen.

EDIT2: Ok I fixed the problem. The problem was that the LinearLayout that hosted the TextViews on top of the actual list had its layout_height attribute set to fill_parent, which strangely was no issue for the non-support version as well as the composer in eclipse, since they both worked that way and I didn't even notice that it was set to fill_parent.

4

3 に答える 3

0

android:id="@+id/android:list" の代わりに android:id="@android:id/list" を使用するのはバターだと思います。また、あなたの場合、ListFragment を拡張するのは本当に役に立ちません。フラグメント化して、expandableList に findViewById を使用します。

変更できますか:

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

に:

public void onActivityCreated(Bundle aSavedInstanceState) {

        super.onActivityCreated(aSavedInstanceState);
fillData();
    }

fillData メソッドが呼び出されていることを確認してください。

于 2012-09-22T13:38:25.003 に答える
0

getListView()in aListFragmentは、具体的には のリストビュー ID を探してい@id/android:listます。あなたのようにそこに「+」を追加しても効果があるかどうかはわかりませんが、最初に試すことです.

また、サポート ライブラリに切り替えたことにも注意してください...適切なメソッド呼び出しをすべて切り替えましたか? たとえば、 andgetFragmentManagerを使用してフラグメントを制御するgetSupportFragmentManager代わりに、 などを使用する必要があります。ActivityFragmentActivity

于 2012-09-22T14:07:27.197 に答える
0

わかりました問題を修正しました。問題は、実際のリストの上にある TextViews をホストする LinearLayout の layout_height 属性が fill_parent に設定されていたことでした。これは奇妙なことに、サポートされていないバージョンと Eclipse のコンポーザーでは問題ではありませんでした。 fill_parent に設定されていることにさえ気づきませんでした。

于 2012-09-25T11:21:47.287 に答える