Android - 选项卡导航<同时跨越新活动>

发布于 2024-09-12 09:31:35 字数 1055 浏览 0 评论 0原文

我定义了一个带有三个选项卡的 HomeActivity,每个选项卡都是一个单独的活动。我使用了 Android 开发者网站中的示例。 http://developer.android.com/resources/tutorials/views/ hello-tabwidget.html

选项卡 B(第二个选项卡)的 UI 包含文本和按钮(名为“搜索”)。当用户单击搜索按钮时,它应该联系 REST Web 服务并将结果显示为列表。

为了做到这一点,在 TAB B 活动内,单击按钮时,我调用一个方法,该方法创建一个意图并调用一个新的 SearchResultsActivity(此后称为 SRA)。在 SRA(扩展列表活动)内,我有连接到 Web 服务并解析返回的 JSON 结果的逻辑,该结果将结果显示为列表。我能够实现此功能。但我看到这里有一些缺点,我的问题是:

  • 定义一个新的活动可以吗(SRA)来处理搜索结果?还是在 TAB B 活动本身中处理它会更好?我选择单独活动的主要原因是,SRA 扩展了 ListActivity,如果我想将其显示为列表,则需要它,而 TabB 只是扩展 Activity 和不允许我显示结果。那么,有更好的方法吗?

  • 鉴于上述实现,当我从 TabB(单击搜索按钮)导航到 SRA 时,选项卡不再可见。由于 TabB 正在调用新活动( Intent srchIntent = new Intent(TabB.this, SearchResultActivity.class); TabB.this.startActivity(srchIntent);),选项卡消失。在这种情况下,最好的解决方案是什么,以便选项卡出现/结果显示在选项卡 B 中?

  • 从 TabB 导航到 SRA 时,我尝试在调用 StartActivity 之前显示在 TabB 中定义的进度对话框/加载,然后取消它。但是加载图标没有出现。我尝试在 SRA 中显示对话框并取消,如下所示嗯。但是没有出现加载。

I have defined a HomeActivity with three tabs and each tab is a seperate activity.I used the example in the android developer site. http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Tab B's (second tab) UI conatins a text and button (named Search).When the user clicks the search button,it should contact a REST webservice and show the results as a list.

In Order to do this,inside TAB B activity,on the click of the button,i call a method which creates an intent and calls a new SearchResultsActivity(referring as SRA henceforth).Inside the SRA(extends List Activity),i have the logic for connecting to the webservice and parsing the JSON result returned which displays the results as a list.I am able to achieve this functionality.But i see some drawbacks here and my questions are:

  • Is it fine to define a new activty (SRA) to handle the search results? or would it be better to have it handled in TAB B activity itself?The main reason why i went for a seperate activity is,the SRA extends ListActivity which would be needed if i want to display it as List and TabB is just extending Activity and wouldnt allowme to display teh results.So,is there a better way to do this?

  • Given the above implementation,when i navigate from TabB (click search button) to SRA,the tabs are not seen anymore.As TabB is calling a new activity(
    Intent srchIntent = new Intent(TabB.this, SearchResultActivity.class);
    TabB.this.startActivity(srchIntent);),the tab goes away.What could be the best solution in this case so that the tabs appear/results shown within Tab B ?

  • While navigating from TabB to SRA,i am trying to show a Progress dialog/Loading defined in TabB before calling StartActivity and cancel it afterwards.But the loading icon does not appear.I have tried showing the dialog and canceling in SRA as well.But the loading does not appear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-09-19 09:31:35

嘿巴拉,我要说的是:

1)最好将 TAB B 扩展为 ListActivity 并由辅助类完成搜索。这样你就可以让你的代码更加独立。

2)实施1),你就会没事的。

3)当你开始请求时,你应该显示一个进度对话框,并在得到结果时停止它。我将实现一个广播接收器来实现这一目标(如果您选择这样做,我可以帮助您)。

Hey Bala, what i have to say is:

1) It would be best to extend the TAB B as a ListActivity and the search done by a helper class. This way you are making your code more independent.

2) Implement 1) and you will be ok.

3) You should show a progress dialog when you start the request, and stop it when you got results. I would implement a broadcast receiver to achieve that (I can help you out, if you choose to do that).

余厌 2024-09-19 09:31:35

有两种方法可以实现此目标。

每当您启动另一个活动(即搜索活动)时,请在此之前将搜索活动设置为所需的选项卡。您可以通过获取 TabActivity 的实例(即扩展此类的活动)并调用 new Intent().setClass(TABACTIVITY_INSTANCE,ACTIVITY YOU_WANT_TO_SET_TO_THIS_TAB) 来实现此目的。但是要使不同的意图对象成为成员类..不要做这样的事情(new Intent().setClass())

将意图对象的数量声明为您持有的选项卡的数量,然后使用 setClass 方法。
(这将解决选项卡消失的问题)

现在,为了从服务器获取数据,我建议您实现 AsyncTask (Android 上提供了很棒的 api):

private class DownloadImageTask extends AsyncTask<String, Void, String> 
{
    AbousUsHandler aboutHandeler;

    @Override
    protected void onPreExecute()
    {
        mProgress.setMessage("Please wait");
        mProgress.show();
        super.onPreExecute();
    }

    protected String doInBackground(String... urls)
    {
        /* Do you your background task i.e. getting data from server
           but don't do ui related things here as this method is called in Thread
           pool not on Android Ui thread.*/

        return null;
    }

    protected void onPostExecute(String result)
    {
        try
        {
            mProgress.dismiss();
            /* set Your List View Adapter here */
        }
    }
}

仅通过调用 新的DownloadImageTask().execute()

首先将调用 preExecute,然后调用 doInBackground,当您从服务器获取数据时,将调用 onPostExecute

我希望这能解决您的问题。

There are two approaches to achieve this goal..

Whenever you make start your another activity (i.e. search activity), just before that set your search activity to your desired tab. You can achieve this by taking a instance of your TabActivity (i.e. your activity which is extending this class) and calling new Intent().setClass(TABACTIVITY_INSTANCE,ACTIVITY YOU_WANT_TO_SET_TO_THIS_TAB). But make to make the different objects of intent as member class .. Don't do something like this (new Intent().setClass()).

Declare the no of intent objects as no of Tabs you are holding and then use setClass method.
(This will solve your problem of the tab disappearing)

Now for taking data from server, I suggest you to implement AsyncTask (wonderful api available on Android):

private class DownloadImageTask extends AsyncTask<String, Void, String> 
{
    AbousUsHandler aboutHandeler;

    @Override
    protected void onPreExecute()
    {
        mProgress.setMessage("Please wait");
        mProgress.show();
        super.onPreExecute();
    }

    protected String doInBackground(String... urls)
    {
        /* Do you your background task i.e. getting data from server
           but don't do ui related things here as this method is called in Thread
           pool not on Android Ui thread.*/

        return null;
    }

    protected void onPostExecute(String result)
    {
        try
        {
            mProgress.dismiss();
            /* set Your List View Adapter here */
        }
    }
}

Execute this from your UI thread only by calling new DownloadImageTask().execute().

First preExecute will be called and then doInBackground and when you get your data from the server, onPostExecute will be called.

I hope this solves your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文