简体   繁体   中英

Display a ListView on button click

I wanted to display a ListView on button click in Android.

_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 Layout

<?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>

Can anyone help me to call listView on a button click? When I clicked on it nothing showed. But the Toast is working. I use a custom ListViewAdapter.

...mmm where do you want to display the ListView? just under or above the button in a LinearLayout?

is _display_all_routes_details defined in your activity Layout??

otherwise you should call addView.

or maybe you could just populate the ListView in the onCreate and change the visibility of the ListView by clicking the button calling

yourListView.setVisibility(View.VISIBLE);

and/or

yourListView.setVisibility(View.GONE);

But if datas that populates the ListView are dynamic you should use a cursor via cursorLoader so that when datas change the View is Updated.

Can you post also the xml layout???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM