如何在一个 Activity 中显示 listview 和 webview?
Posted
技术标签:
【中文标题】如何在一个 Activity 中显示 listview 和 webview?【英文标题】:How to display a listview and a webview in one Activity? 【发布时间】:2016-08-30 02:51:36 【问题描述】:所以我试图在一个 Activity 中实现两个 API,并在同一个 Activity 中为我的第二个 API 显示 listview 和 webview。我设法让列表视图下来。这是我尝试调用的 MainActivity 的一部分,通过 onClick 方法运行两个 API
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.twit_list);
activity = this;
Key = getStringFromManifest("CONSUMER_KEY");
Secret = getStringFromManifest("CONSUMER_SECRET");
txtSearch = (EditText) findViewById(R.id.txtSearch);
searchbtn = (Button) findViewById(R.id.searchbtn);
save = (Button) findViewById(R.id.save);
savedSearches = (Button)findViewById(R.id.savedSearches);
searchbtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
downloadSearches();
new GoogleSearch();
);
save.setOnClickListener(new Button.OnClickListener()
@Override
public void onClick(View v)
saveSearch();
);
savedSearches.setOnClickListener(new Button.OnClickListener()
@Override
public void onClick(View v)
openSavedSearches();
);
这是 GoogleSearch API public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.twit_list);
txtSearch = (EditText)webView.findViewById(R.id.txtSearch);
searchbtn = (Button) webView.findViewById(R.id.searchbtn);
webView = (WebView)webView.findViewById(R.id.webView);
searchbtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String item = txtSearch.getText().toString();
new JsonSearchTask(item).execute();
);
private class JsonSearchTask extends AsyncTask<Void, Void, Void>
String searchResult = "";
String search_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String search_query;
JsonSearchTask(String item)
try
search_item = URLEncoder.encode(item, "utf-8");
catch (UnsupportedEncodingException e)
// TODO Auto-generated catch block
e.printStackTrace();
search_query = search_url + search_item;
@Override
protected Void doInBackground(Void... arg0)
try
searchResult = ParseResult(sendQuery(search_query));
catch (JSONException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
return null;
@Override
protected void onPreExecute()
searchbtn.setEnabled(false);
searchbtn.setText("Wait...");
super.onPreExecute();
@Override
protected void onPostExecute(Void result)
webView.loadData(searchResult,
"text/html",
"UTF-8");
searchbtn.setEnabled(true);
searchbtn.setText("Search");
super.onPostExecute(result);
private String sendQuery(String query) throws IOException
String result = "";
URL searchURL = new URL(query);
HttpURLConnection httpURLConnection = (HttpURLConnection) searchURL.openConnection();
if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader,
8192);
String line = null;
while((line = bufferedReader.readLine()) != null)
result += line;
bufferedReader.close();
return result;
private String ParseResult(String json) throws JSONException
String parsedResult = "";
JSONObject jsonObject = new JSONObject(json);
JSONObject jsonObject_responseData = jsonObject.getJSONObject("responseData");
JSONArray jsonArray_results = jsonObject_responseData.getJSONArray("results");
//parsedResult += "Google Search APIs (JSON) for : <b>" + search_item + "</b><br/>";
//parsedResult += "Number of results returned = <b>" + jsonArray_results.length() + "</b><br/><br/>";
for(int i = 0; i < jsonArray_results.length(); i++)
JSONObject jsonObject_i = jsonArray_results.getJSONObject(i);
String iTitle = jsonObject_i.getString("title");
String iContent = jsonObject_i.getString("content");
String iUrl = jsonObject_i.getString("url");
parsedResult += "<a href='" + iUrl + "'>" + iTitle + "</a><br/>";
parsedResult += iContent + "<br/><br/>";
return parsedResult;
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_
android:id = "@+id/activitymain"
>
<RelativeLayout
android:layout_
android:layout_
android:orientation="horizontal">
<EditText
android:layout_
android:layout_
android:id = "@+id/txtSearch"/>
<Button
android:layout_
android:layout_
android:layout_below="@+id/txtSearch"
android:text="Search"
android:id="@+id/searchbtn" />
<Button
android:layout_
android:layout_
android:layout_below="@+id/txtSearch"
android:layout_toRightOf="@+id/searchbtn"
android:layout_toEndOf="@+id/searchbtn"
android:id="@+id/save"
android:text=" Save " />
<Button
android:layout_
android:layout_
android:text="Saved Searches"
android:id="@+id/savedSearches"
android:layout_alignTop="@+id/save"
android:layout_toRightOf="@+id/save"
android:layout_toEndOf="@+id/save" />
</RelativeLayout>
<ListView
android:layout_
android:layout_
android:id = "@android:id/list"
android:background="#FF498CDE">
</ListView>
<WebView
android:layout_
android:layout_
android:id="@+id/webView"
android:layout_gravity="center_horizontal" />
基本上我想在单击搜索按钮时同时运行这两个 api。 请有人指出我正确的方向,我对此完全陌生。 谢谢
【问题讨论】:
【参考方案1】:问题是您在 JsonSearchTask AsyncTask 中没有得到任何响应。
问题是您用于谷歌网络搜索的 api 现在不可用。您应该使用 Google 自定义搜索 API (https://developers.google.com/custom-search/)。
【讨论】:
以上是关于如何在一个 Activity 中显示 listview 和 webview?的主要内容,如果未能解决你的问题,请参考以下文章
如何在activity中获取fragment的控件,然后修改控件的内容,
自定义 ListView 以处理从另一个 Intent 传递的 EditText 和 TextView