ListView自定义布局setText不起作用

时间:2013-05-30 14:34:23

标签: android listview layout-inflater settext inflate

我正在尝试在自定义TextViews布局中设置一些ListView的文字。我正在使用ListFragment BTW。 我将在ListView中返回实际的OnCreateView布局,并且我正在对自定义布局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也没有任何变化。 我错过了什么?

布局文件:

ListView Main:

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

CustomLayout:

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

0 个答案:

没有答案