0

我正在尝试TextViews在我的自定义ListView布局中设置一些文本。我正在使用ListFragment顺便说一句。我正在返回实际的ListViewLayout inOnCreateView并且我正在夸大 Custom Layout ind OnActivityCreated。真的不知道这是否是要走的路。

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {


            return inflater.inflate(R.layout.activity_db_list_view, container, false);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.activity_db_row, null, true);


            TextView tv = (TextView) rowView.findViewById(R.id.lv_tprice_unit);
            tv.setText("bla");

final String[] fromColumn = new String[]{DbAdapter.KEY_DATE, DbAdapter.KEY_AMOUNT, DbAdapter.KEY_MILEAGE, DbAdapter.KEY_PRICE, DbAdapter.KEY_TOTAL_PRICE};
        final int[] toViews = new int[]{R.id.lv_date, R.id.lv_amount, R.id.lv_mileage, R.id.lv_lprice, R.id.lv_tprice};

        // Create an empty adapter we will use to display the loaded data.
        // We pass null for the cursor, then update it in onLoadFinished()
        mAdapter = new SimpleCursorAdapter(getActivity(),
                R.layout.activity_db_row, null,
                fromColumn, toViews, 0);
        setListAdapter(mAdapter);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);

    }

    // Called when a new Loader needs to be created
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        return new CursorLoader(getActivity(), DbAdapter.CONTENT_URI,
                PROJECTION, SELECTION, null, DbAdapter.KEY_ID + " DESC");
    }

    // Called when a previously created loader has finished loading
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
    }

    // Called when a previously created loader is reset, making the data unavailable
    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
    }

我没有得到任何NullPointerException东西,但我的也没有任何变化TextView。我错过了什么?

布局文件:

列表视图主要:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/listContainer"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             style="@style/AppBaseThemeCardBG">

    <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            android:divider="#00000000"/>

</FrameLayout>

自定义布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              style="@style/AppBaseThemeCardBG"
              xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
            style="@style/nowCardStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="@dimen/card_layout_padding"
            tools:ignore="UselessParent">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="@dimen/page_margin"
                android:paddingRight="@dimen/page_margin">

            <TextView
                    android:id="@+id/lv_date"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="4"
                    android:fontFamily="sans-serif-light"
                    android:gravity="left"
                    android:padding="@dimen/activity_listview_padding"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_tprice"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:fontFamily="sans-serif-light"
                    android:gravity="right"
                    android:padding="@dimen/activity_listview_padding"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_tprice_unit"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="left"
                    android:padding="@dimen/activity_listview_padding"
                    android:text="@string/money_unit"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>
        </LinearLayout>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="@dimen/page_margin"
                android:paddingRight="@dimen/page_margin">

            <TextView
                    android:id="@+id/lv_mileage"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="right"
                    android:padding="@dimen/activity_listview_padding"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_mileage_unit1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="left"
                    android:padding="@dimen/activity_listview_padding"
                    android:text="@string/mileage_unit"/>

            <TextView
                    android:id="@+id/lv_lprice"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:fontFamily="sans-serif-light"
                    android:gravity="right"
                    android:padding="@dimen/activity_listview_padding"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_lprice_unit"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="left"
                    android:padding="@dimen/activity_listview_padding"
                    android:text="@string/lprice_unit"/>
        </LinearLayout>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="@dimen/page_margin"
                android:paddingRight="@dimen/page_margin">

            <TextView
                    android:id="@+id/lv_distdif"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="right"
                    android:maxLines="1"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_mileage_unit"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="left"
                    android:padding="@dimen/activity_listview_padding"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_amount"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:fontFamily="sans-serif-light"
                    android:gravity="right"
                    android:padding="@dimen/activity_listview_padding"
                    android:textIsSelectable="false"/>

            <TextView
                    android:id="@+id/lv_amount_unit"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:fontFamily="sans-serif-light"
                    android:gravity="left"
                    android:padding="@dimen/activity_listview_padding"
                    android:text="@string/amount_unit"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
4

0 回答 0