单击按钮时Android创建ListView

时间:2012-09-13 09:08:44

标签: android android-layout listview android-widget

我正在尝试创建搜索活动。布局包含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;

}

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

2 个答案:

答案 0 :(得分:0)

您应首先创建一个arraylist来从查询中获取记录,

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

答案 1 :(得分: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&amp; Search Button)和ListView放入其中。

感谢您的时间。