Rudolf Gröhling
Rudolf Gröhling

Reputation: 4825

Android | TextView.setText() not work

So I have such quite complicated fragment. It is fragment used in ViewPager and it's populated by own CursorLoader and also contains some ListFragments added via getChildFragmentManager().

In onCreate() I assign view elements and initiat Loader like so

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

    final View v = inflater.inflate(R.layout.user_account, container, false);

    mUserName = (TextView) v.findViewById(R.id.user_account_full_name);
    mUserLastSeen = (TextView) v.findViewById(R.id.user_account_user_last_seen);
    mCreditBalance = (TextView) v.findViewById(R.id.user_account_credit_balance);
    ...

    // then I populate FrameLayouts with ListViewFragments
    FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.add(R.id.user_account_credit_history, CreditListFragment.newInstance(mUserId));
    ft.add(R.id.user_account_bill_consumption, ConsumptionListFragmentAccount.newInstance(mUserId));
    ft.commit();

    // initiating the Loader
    getActivity().getSupportLoaderManager().initLoader(0, null, this);

    return v;
}

and finally in onLoadFinished() I want populate my view

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    data.moveToFirst();
    Log.e("TAG", data.getString(UserId.NAME));
    mUserName.setText(data.getString(UserId.NAME));
    mUserLastSeen.setText(getString(R.string.last_seen) + ": " +
            (data.isNull(UserId.LAST_VISIT) ? getString(R.string.never) :
                    String.format("%te. %<tB %<tY", data.getLong(UserId.LAST_VISIT))));
    Utils.setColorCurrency(mCreditBalance, data.getFloat(UserId.ACCOUNT_BALANCE));
}

but the View is not populated. The Log.e(...) writes user name as expected, so the Loader is initiated ad has valid data, but none of my TextViews is replaced with the data, which is very, very strange.

Any ideas?


EDIT/ Got it!

Solution moved to answer. Thanks all, especially to Shoeb Siddique, for your help! You are a star!

Upvotes: 2

Views: 3190

Answers (3)

Rudolf Gr&#246;hling
Rudolf Gr&#246;hling

Reputation: 4825

Because of the ViewPager there were numerous sibling Fragments and I used the same loaderId = 0 in all of them.

If loader with the same Context, loaderId and LoaderCallbacks exists, LoaderManager.initLoader() returns the already created loader, so all my Fragments shared one Loader.

The Fragment, that initiated the Loader firs, lets call it clickFragment, was not populated by the LoaderCallbacks and so was all Fragments left from the clickFragment, while all Fragments right/next to clickFragment were populated with data of clickFragment. So the clickFragment initiated the Loader and as the ViewPager prepared the sibling Fragments, the data were delivered to all right Fragments. Strangely, the clickFragment itself was not populated and I did not slide the view to siblings, so I saw just empty View and was confused.

Now I generate unique loaderId for each fragment and it finally works.

Upvotes: 0

Shoeb Siddique
Shoeb Siddique

Reputation: 2825

  mUserName = (TextView) v.findViewById(R.id.user_account_full_name);
    handler.post(new Runnable() {
                public void run() { 

                 mUserName.setText(data.getString(UserId.NAME));
        mUserLastSeen.setText(getString(R.string.last_seen) + ": " +
                (data.isNull(UserId.LAST_VISIT) ? getString(R.string.never) :
                        String.format("%te. %<tB %<tY", data.getLong(UserId.LAST_VISIT))));
        Utils.setColorCurrency(mCreditBalance, data.getFloat(UserId.ACCOUNT_BALANCE));
                } 
            }); 

Upvotes: 2

Imanali Mamadiev
Imanali Mamadiev

Reputation: 2654

You can use User Interface thread runOnUiThread:

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    data.moveToFirst();
    Log.e("TAG", data.getString(UserId.NAME));
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            mUserName.setText(data.getString(UserId.NAME));
            mUserLastSeen.setText(getString(R.string.last_seen) + ": " +
                    (data.isNull(UserId.LAST_VISIT) ? getString(R.string.never) :
                            String.format("%te. %<tB %<tY", data.getLong(UserId.LAST_VISIT))));
        }
    });

    Utils.setColorCurrency(mCreditBalance, data.getFloat(UserId.ACCOUNT_BALANCE));
}

Upvotes: 1

Related Questions