我正在使用google搜索实现来搜索用户单击按钮时输入的结果。
我听这个按钮,点击并获取/解析JSON结果。
我已经解析了JSON的"title“和"url”,并希望将其填充到ListView
中。
但是,我认为我没有正确地使用ArrayAdapter
类。
MyActivity.java:
ListView listViewGoogle;
private String[] resultArray;
private EditText searchBox;
private Button searchBtn;
private String search_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
private String search_query;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
listViewGoogle = (ListView)findViewById(R.id.listviewgoogle);
searchBox = (EditText)findViewById(R.id.searchBox);
searchBtn = (Button)findViewById(R.id.searchBtn);
searchBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new JsonSearchTask().execute();
try
{
search_query = search_url + searchBox.getText().toString();
new JsonSearchTask().execute();
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listviewgoogle, resultArray);
listViewGoogle.setAdapter(adapter);
}
catch (Exception e)
{
// TODO: handle exception
}
}
});
}
private class JsonSearchTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... arg0)
{
try
{
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;
}
}
private String sendQuery(String query) throws IOException
{
String queryResult = "";
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)
{
queryResult += line;
}
bufferedReader.close();
}
return queryResult;
}
private void 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");
resultArray = new String[jsonArray_results.length()];
for(int i = 0; i < jsonArray_results.length(); i++)
{
JSONObject jsonObject_i = jsonArray_results.getJSONObject(i);
parsedResult = "title: " + jsonObject_i.getString("title") + "\n";
parsedResult += "url: " + jsonObject_i.getString("url") + "\n";
//parsedResult += "\n";
resultArray[i] = parsedResult;
}
}
activity_listviewgoogle.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:gravity="center" >
</TextView>
activity_my.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/parentOuter" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:hint="type to search..."
android:layout_weight="2"
android:id="@+id/searchBox" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Search Me!"
android:layout_weight="2"
android:id="@+id/searchBtn" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listviewgoogle"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
我好像收到了这个错误:
错误:(71,44)错误:没有为ArrayAdapter(,int,String[])构造函数找到合适的构造函数ArrayAdapter.ArrayAdapter(上下文、int、int)不适用(参数不匹配;不能转换为上下文)构造函数ArrayAdapter.ArrayAdapter(上下文、int、String[])不适用(参数不匹配;不能转换为上下文)构造函数ArrayAdapter.ArrayAdapter(上下文、int、列表)不适用(参数不匹配;不能转换为上下文)
是否有一种使用ArrayAdapter填充数据的ListView
的方法,或者有其他方法?
我很抱歉,我是机器人新手。
发布于 2015-08-27 19:23:42
你做错了。您已经调用了AsyncTsk并设置了适配器,如:
new JsonSearchTask().execute();
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listviewgoogle, resultArray);
listViewGoogle.setAdapter(adapter);
JsonSearchTask
在独立线程中运行,而listViewGoogle.setAdapter(adapter);
在获得JsonSearchTask
结果之前在主线程中调用。因此,您必须在onPostExecute()
方法JsonSearchTask
中调用此方法。
错误:
错误:(71,44)错误:没有为ArrayAdapter(,int,String[])构造函数找到合适的构造函数ArrayAdapter.ArrayAdapter(上下文、int、int)不适用(参数不匹配;不能转换为上下文)构造函数ArrayAdapter.ArrayAdapter(上下文、int、String[])不适用(参数不匹配;不能转换为上下文)构造函数ArrayAdapter.ArrayAdapter(上下文、int、列表)不适用(参数不匹配;不能转换为上下文)
你这样打电话:
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listviewgoogle, resultArray);
在你的setOnClickListener里。因此,当时this
引用的是OnClickListener的上下文引用,而不是您的活动引用。
所以试试
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.activity_listviewgoogle, resultArray);
而不是
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listviewgoogle, resultArray);
希望能帮上忙!
发布于 2015-08-27 19:00:07
ArrayAdapter的第一个参数应该是使用ActivityName.this的上下文...try,其中ActivityName是您的活动名.
对于您的键盘,我认为问题在于xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentOuter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="type to search..." />
<Button
android:id="@+id/searchBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Me!" />
<ListView
android:id="@+id/listviewgoogle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
https://stackoverflow.com/questions/32262645
复制