创建 Android 搜索结果列表
我正在尝试实现以下逻辑:
用户在文本框(活动)中输入搜索字符串 ->调用Web服务异步执行搜索->搜索结果显示在列表中(另一个活动)
到目前为止,我有以下内容:
输入搜索字符串并单击“搜索”按钮的活动:
public class Search extends Activity
{
// ...
// called when "Search" button is clicked
private void runSearch()
{
ProgressDialog progressDialog = ProgressDialog.show(
this, "Search", "Search...");
searchAsyncTask = new SearchAsyncTask();
SearchAsyncTaskParam param = new SearchAsyncTaskParam();
param.SearchString = getSearchCode(); // gets input from text box
param.ProgressDialog = progressDialog;
searchAsyncTask.execute(param);
}
}
然后我有一个 >执行异步搜索的类:
public class SearchAsyncTask extends AsyncTask<SearchAsyncTaskParam,
Void, SearchAsyncTaskResult> {
private SearchAsyncTaskParam param;
@Override
protected SearchAsyncTaskResult doInBackground(
SearchAsyncTaskParam... params) {
if (params.length > 0)
param = params[0];
SearchAsyncTaskResult result = new SearchAsyncTaskResult();
// call Webservice and fill result object with status (success/failed)
// and a list of hits (each hit contains name, city, etc.)
return result;
}
@Override
protected void onPostExecute(SearchAsyncTaskResult result) {
param.ProgressDialog.dismiss();
if (!result.Success)
// throw an AlertBox
else
{
// this part is incomplete and doesn't look good, does it?
// And how would I pass my result data to the new activity?
Intent intent = new Intent(param.ProgressDialog.getContext(),
SearchResultList.class);
param.ProgressDialog.getContext().startActivity(intent);
}
}
}
最后一个元素是一个显示搜索结果列表的活动:
public class SearchResultList extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, data));
// String was only for testing, should be a class with holds the data
// for each item, i.e. Name, City, etc. And where do I get "data" from?
}
// ...
}
现在我的问题:
是最好在
AsyncTask
实例的onPostExecute
方法?我已经用上面概述的简单测试对其进行了测试,它似乎有效。但这安全吗?我是否处于正确的线程(UI 线程)上?如果这是不好的做法,启动结果活动的替代方法是什么?使用
ProgressDialog
的上下文作为Intent
并启动 Activity 看起来特别奇怪。它只是我在 AsyncTask 实例中可用的唯一上下文。这会是一个问题吗,因为ProgressDialog
已经被关闭了?我还可以使用什么上下文?如何将结果数据传递到
SearchResultList
活动中?如果搜索成功(上面的
onPostExecute
方法中的else
情况),我想使用以下命令关闭Search
活动文本框,这样,如果点击后退按钮,用户将返回主屏幕(从中打开搜索活动),而不是Search
活动。这可能吗?我该如何做到这一点?
I am trying to implement the following logic:
User enters a search string in a text box (an activity) -> Web service is called to perform the search asynchronously -> Search results are displayed in a list (another activity)
So far I have the following:
An activity to enter the search string and click a "Search" button:
public class Search extends Activity
{
// ...
// called when "Search" button is clicked
private void runSearch()
{
ProgressDialog progressDialog = ProgressDialog.show(
this, "Search", "Search...");
searchAsyncTask = new SearchAsyncTask();
SearchAsyncTaskParam param = new SearchAsyncTaskParam();
param.SearchString = getSearchCode(); // gets input from text box
param.ProgressDialog = progressDialog;
searchAsyncTask.execute(param);
}
}
Then I have a class to perform the asynchronous search:
public class SearchAsyncTask extends AsyncTask<SearchAsyncTaskParam,
Void, SearchAsyncTaskResult> {
private SearchAsyncTaskParam param;
@Override
protected SearchAsyncTaskResult doInBackground(
SearchAsyncTaskParam... params) {
if (params.length > 0)
param = params[0];
SearchAsyncTaskResult result = new SearchAsyncTaskResult();
// call Webservice and fill result object with status (success/failed)
// and a list of hits (each hit contains name, city, etc.)
return result;
}
@Override
protected void onPostExecute(SearchAsyncTaskResult result) {
param.ProgressDialog.dismiss();
if (!result.Success)
// throw an AlertBox
else
{
// this part is incomplete and doesn't look good, does it?
// And how would I pass my result data to the new activity?
Intent intent = new Intent(param.ProgressDialog.getContext(),
SearchResultList.class);
param.ProgressDialog.getContext().startActivity(intent);
}
}
}
And the last element is an activity to display a list with the search results:
public class SearchResultList extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, data));
// String was only for testing, should be a class with holds the data
// for each item, i.e. Name, City, etc. And where do I get "data" from?
}
// ...
}
Now my questions:
Is it a good idea to start the activity for the result list in the
onPostExecute
method of theAsyncTask
instance? I have tested it with the simple test sketched above and it seems to work. But is this safe, am I on the right thread (UI thread) at all? If this is bad practice what is the alternative to start the result activity?It looks especially weird to use the context of the
ProgressDialog
for theIntent
and to start the activity. It is just the only context I have available in theAsyncTask
instance. Can this be a problem because theProgressDialog
is already dismissed? What context else could I use?How do I pass my result data into the
SearchResultList
activity?In case of a successful search (the
else
case in theonPostExecute
method above) I would like to close theSearch
activity with the text box so that, if the back button is hit, the user returns to the main screen (which the search activity was opened from) and not theSearch
activity. Is this possible and how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,因为您位于 UI 线程上。另一种方法是如果您不使用 AsyncTask 而只是使用一个简单的线程。在这种情况下,您将使用 Handler 来通知 UI 线程工作已完成。这是 Asynctask 存在的原因之一,因此您不必执行后一个版本。
“使用 ProgressDialog 的上下文看起来特别奇怪
的意图并开始活动。”
这一点也不奇怪,因为当您创建 ProgressDialog 时,您传递了 Search 的上下文,因此当您检索 ProgressDialog 的上下文时,您实际上获得了 Search 活动的上下文。完全没问题。
然后在 SearchResultList 活动中您将执行以下操作:
如果您想传递自己的对象,请参阅此问题:如何在Android中声明全局变量?
可能吗?是的。对于您的情况,有两种方法可以做到这一点。您可以像我一样,将 SearchAsyncTask 编写为 Search 类中的内部类,然后直接调用
如果某些条件成立,或者您可以查看此问题已接受的答案:android如何从其他活动中完成一个活动
祝你好运!
Yes, because you are on the UI thread. The alternative would be if you aren't using AsyncTask but just a simple thread. In that case you would use a Handler to notify the UI thread that work is complete. This is one of the reasons Asynctask exists, so you don't have to do the latter version.
It's not weird at all because when you created the ProgressDialog you passed the context of Search, so when you retrieve the context of the ProgressDialog you actually get the context of the Search activity. No problem at all.
This depends on what data you want to pass. If you just want to pass an array of Strings you could do it like this:
Then in the SearchResultList activity you would do:
If you would like to pass an object of your own, see this question: How to declare global variables in Android?
Is it possible? Yes. There's two ways to do this in your case. Either you can, as I would do it, write the SearchAsyncTask as a inner class in your Search class and the just call
If some condition holds, or you can check out the accepted anwser to this question: android how to finish an activity from other activity
Good Luck!