2012-09-13 170 views
2

我正在尝试创建搜索活动。该布局包含EditText(用于键入搜索查询)Button & ListView(用于显示搜索结果)。 search_layout.xml(对于搜索框显示& ListView)情况如下:Android在按钮上单击创建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; 

} 

该代码运行良好。我从Web服务获取搜索结果。但是看不到下面的任何列表。任何想法我搞砸了?

+0

任何人谁可以回答Android的问题? – hemu

+0

listadapter中的元素是什么?显示'doSearch'中的一些代码 –

+0

@AleksG - 添加的代码doSearch() – hemu

回答

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在它。

谢谢你的时间。

0

,必须先创建一个数组LIS获取从查询recorde,

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); 
+0

准备清单和适配器的代码在doSearch方法中。我只是从doSearch返回适配器 – hemu

+0

为什么你使用_layout xml_。这种类型的xmls用于定制列表,而不用于默认列表。 –

+0

然后我们如何在ListView中显示多个项目(在我的例子中为$ items:Name,city,email phone)? – hemu