单击按钮显示 ListView

Display a ListView on button click

我想在 Android 中单击按钮时显示 ListView。

_btn_show_details = (Button) findViewById(R.id.btn_showdetails);
        _btn_show_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Toast.makeText(getApplication(),"i am cliked",Toast.LENGTH_SHORT).show();

                list = new ArrayList<HashMap<String, String>>();
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("StopNames", StopElement.Stop_name_list.toString());
                map.put("RouteNo", DisplayAllRoutesActivity.getrouteno.toString());
                list.add(map);

                ListViewAdapter adapter=new ListViewAdapter(DisplayAllRouteDetailActivity.this, list);
                _display_all_routes_details.setAdapter(adapter);

            }
        });

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.google.android.gms.maps.MapFragment"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

                <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_marginTop="5dp"
                            android:paddingTop="5dp"
                            android:paddingLeft="5dp"
                            android:layout_height="20dp"
                            android:gravity="start"
                            android:id="@+id/routefare_id"
                            android:background="#81c784"
                            android:text=""
                            android:textColor="#000"
                            android:textSize="12sp" />


                        <TextView
                       android:layout_width="match_parent"
                       android:layout_marginTop="25dp"
                       android:paddingTop="5dp"
                       android:layout_height="30dp"
                       android:gravity="center"
                       android:id="@+id/routename_id"
                       android:background="#81c784"
                       android:text=""
                       android:textColor="#000"
                       android:textSize="14sp" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_marginTop="50dp"
                            android:layout_marginLeft="30dp"
                            android:paddingTop="5dp"
                            android:layout_height="25dp"
                            android:gravity="start"
                            android:id="@+id/routeno_id"
                            android:background="#81c784"
                            android:text=""
                            android:textColor="#000"
                            android:textSize="14sp" />


                        <Button
                            android:layout_width="100dp"
                            android:layout_height="20dp"
                            android:text="View Details"
                            android:gravity="center"
                            android:layout_marginTop="80dp"
                            android:layout_marginLeft="120dp"
                            android:textSize="12sp"
                            android:id="@+id/btn_showdetails"
                            android:layout_gravity="center"
                            android:background="#81c784" />


                        <ListView
                            android:id="@+id/listView1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        </ListView>


                    </RelativeLayout>
            </FrameLayout>

        </fragment>
        </RelativeLayout>
</LinearLayout>

任何人都可以帮助我在单击按钮时调用 listView 吗?当我点击它时,没有任何显示。但是 Toast 正在工作。我使用自定义 ListViewAdapter。

...嗯 你想在哪里显示ListView? 就在 LinearLayout 中按钮的下方或上方?

是 _display_all_routes_details 在您的 activity 布局中定义的吗??

否则你应该调用 addView。

或者您可以只在 onCreate 中填充 ListView 并更改 通过单击调用按钮显示 ListView 的可见性

yourListView.setVisibility(View.VISIBLE);

and/or

yourListView.setVisibility(View.GONE);

但是如果填充 ListView 的数据是动态的,您应该通过 cursorLoader 使用游标,以便在数据更改时更新视图。

你能post也xml布局吗???