我正在尝试创建搜索活动。布局包含EditText
(键入搜索查询)Button
和ListView
(显示搜索结果)。search_layout.xml(用于显示搜索框 & ListView
)如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText_search"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/search_practice" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_search"
android:onClick="searchPractice" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/praclist"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
列表项 list_items.xml 的布局是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="6dip"
android:paddingBottom="2dip" />
<!-- Description label -->
<TextView
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<!-- Linear layout for cost and price Cost: Rs.100 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Cost Label -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:gravity="left"
android:textStyle="bold"
android:text="Phone: " >
</TextView>
<!-- Price Label -->
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:textStyle="bold"
android:gravity="left">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
在按钮单击(searchBtnClick)上,我正在调用方法 doMySearch :
public void searchBtnClick(View view){
-------
------
ListAdapter adapter = doSearch(query);
ListView pracListView = (ListView) findViewById(R.id.praclist);
PracListView.setAdapter(adapter);
return;
}
private ListAdapter doSearch(String query){
ListAdapter adapter = null;
String url = "https://mywebservice.com/apps/api/v1/search/practices?val="+query+"&by=nam";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
String jsonResponsse = null;
try{
HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
HttpEntity httpEntity = httpResponse.getEntity();
jsonResponsse = getASCIIContentFromEntity(httpEntity);
Gson gson = new Gson();
PracticesByName practicesByName = gson.fromJson(jsonResponsse,PracticesByName.class);
ArrayList<Practices> practiceList = (ArrayList<Practices>)practicesByName.getPractices();
for(int i=0 ; i < practiceList.size() ; i ++){
Object a = practiceList.get(i);
System.out.println(a.getClass() + " " + a.toString());
Practices aPractice = practiceList.get(i);
HashMap<String, String> aMap = new HashMap<String, String>();
aMap.put(TAG_NAME, aPractice.getName());
aMap.put(TAG_CITY, aPractice.getCity());
aMap.put(TAG_EMAIL, aPractice.getEmail());
aMap.put(TAG_PHONE, aPractice.getPhone());
searchResultList.add(aMap);
}
adapter = new SimpleAdapter(this, searchResultList,
R.layout.list_item_prac_search,
new String[] { TAG_NAME, TAG_CITY, TAG_EMAIL, TAG_PHONE}, new int[] {
R.id.name, R.id.city, R.id.email, R.id.phone});
//pracListView.setAdapter(adapter);
}catch (Exception e){
System.out.println("Exception while getting serach for practice");
e.printStackTrace();
}
return adapter;
}
代码运行良好。我正在从网络服务获取搜索结果。但看不到下面的任何列表。知道我在搞砸什么吗?