2

我正在尝试创建搜索活动。布局包含EditText(键入搜索查询)ButtonListView(显示搜索结果)。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;

}

代码运行良好。我正在从网络服务获取搜索结果。但看不到下面的任何列表。知道我在搞砸什么吗?

4

2 回答 2

0

您应该首先创建一个数组 lis 从查询中获取记录,

ArrayList<String> items = new ArrayList<String>();
        items.add(doSearch(query));

然后替换这一行,

ListAdapter adapter = doSearch(query);

有了这条线,

ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,items);
ListView pracListView = (ListView) findViewById(R.id.praclist);
  PracListView.setAdapter(adapter);
于 2012-09-13T10:15:53.800 回答
0

最后,得到它的工作。更正了 search_layout.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="vertical">

    <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"
            android:imeOptions="actionDone" />

         <Button 
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:text="@string/button_search"
             android:onClick="searchPractice" />         
    </LinearLayout>

    <ListView 
            android:id="@+id/praclist"
            android:layout_weight="1"
            android:layout_width="match_parent"            
            android:layout_height="wrap_content" />

</LinearLayout>

使外部布局垂直并放置水平布局(其中包含 EditText 和搜索按钮)和 ListView。

谢谢你的时间。

于 2012-09-13T13:23:44.370 回答