2

I'm trying to do something very simple. I have 2 tabs, one showing information about the breed of a dog and the other is showing a list of reviews that people have written describing the breed. I would like to include the number of reviews that were written in the Tab as well so it would look like so:

Breed Info | Reviews(19)

The issue is that I need to download said review data first in order to see the number of reviews there is on the tab.

So what I thought of as a solution was to add the tab first in my BreedProfile.java (ShelockActivity)

ActionBar.Tab reviewTab= ab.newTab();
reviewTab.setText("Reviews");
mTabsAdapter.addTab(reviewTab, DogReviewFragment.class, null);

Then in my BreedProfileReviewFragment.java I would download the reviews then update the reviewTab text by doing so:

getActivity().getActionBar().getTabAt(1).setText("Reviews 10");

However, the above method does not exist according to the LogCat.

03-05 18:15:26.460: E/AndroidRuntime(1286): FATAL EXCEPTION: main
03-05 18:15:26.460: E/AndroidRuntime(1286): java.lang.NoSuchMethodError: android.support.v4.app.FragmentActivity.getActionBar

So my question is, is it possible to access the TAB from the FRAGMENT to change the TEXT property of the TAB after it is added? If it is, how can I achieve that?

NOTES:

  1. I cannot download the reviews together with the breed information because the API's are separate.

  2. The BreedProfile.java Originates from a list of Breeds in a ListActivity BreedList.java.

4

1 回答 1

2

您可以实现一个Interface. 因此您可以在片段的父活动中运行代码。

在您的片段中创建您的界面,如下所示:

public interface OnQuestionSelectedListener {
 void onQuestionSelected(int Position, boolean isFirstTime);
}

在您的父活动端实现它,如下所示:

public class YourParentActivity extends Activity implements YourFragment.OnQuestionSelectedListener{
       @Override
    public void onQuestionSelected(int position, boolean isFirstTime) {
        // your code.
         }
}

从您的片段中调用它,如下所示:

((YourParentActivityName)getActivity()).onQuestionSelected(position, true);

(您可以根据需要对代码进行更改)
将所有片段逻辑转移到其父级是一个非常好的做法,因为我认为片段用于您的 UI 部分,并Interface为我们提供了一种转移片段的好方法向父级发送消息。在父级执行所有活动并将结果传输到相应的片段。

希望这会有所帮助。

于 2013-03-05T11:57:00.633 回答