【问题标题】:Android Create ListView on button clickAndroid 在按钮单击时创建 ListView
【发布时间】:2012-09-06 08:17:23
【问题描述】:

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

}

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

【问题讨论】:

  • 有谁能回答安卓问题吗?
  • listadapter 中有哪些元素?显示doSearch中的部分代码
  • @AleksG - 添加了 doSearch() 的代码
  • 这些是首先准备列表视图然后搜索该列表视图的示例。我正在搜索(调用 Web 服务以获取搜索数据)然后在 ListView 中显示搜索结果。

标签: android android-layout listview android-widget


【解决方案1】:

您应该首先创建一个数组 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);

【讨论】:

  • 准备列表和适配器的代码在doSearch方法中。我只是从 doSearch 返回适配器
  • 你为什么使用 layout xml 。这种类型的 xmls 用于自定义列表,而不是默认列表。
  • 那么我们如何在 ListView 中显示多个项目(在我的例子中是 $ items : Name, city, email phone)?
  • 我猜问题出在 search_layout.xml 。我搞砸了。如果你能弄清楚那是什么,那将是一个很大的帮助..Thanx..
  • 你已经搞砸了。现在告诉你想要的清单。您是否希望将每条单独的记录放在一行中。喜欢我的名字、城市、电话、电子邮件在第一行,你的名字和其他详细信息在第二行。??
【解决方案2】:

终于,搞定了。更正了 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 和 Search Button)和 ListView。

感谢您的宝贵时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多