【问题标题】:Display a ListView on button click单击按钮时显示 ListView
【发布时间】:2015-07-01 13:14:33
【问题描述】:

我想在 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 吗?当我点击它时,什么都没有显示。但是吐司正在工作。我使用自定义 ListViewAdapter。

【问题讨论】:

  • _display_all_routes_details.setVisibility(View.Visible);
  • @ANoop M thnkss 成功了 :)

标签: android listview onclicklistener android-button


【解决方案1】:

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

_display_all_routes_details 是否在您的活动布局中定义??

否则你应该调用 addView。

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

yourListView.setVisibility(View.VISIBLE);

和/或

yourListView.setVisibility(View.GONE);

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

你能发布xml布局吗???

【讨论】:

  • 我的数据是动态的 @guageinvariante setadapter 正在崩溃
猜你喜欢
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多