如果ListView适配器中没有数据,我想显示刷新Button和TextView.我还希望能够向将重新加载列表的按钮添加单击侦听器.以下是我定义当前活动的方式:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foods);
arrList=new ArrayList();
listView=(ListView)findViewById(R.id.list_foods);
this.listView.setEmptyView(findViewById(R.id.emptyView));
.....
.....
}
这是我的xml文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
我在哪里设置刷新按钮的点击监听器?
解决方法:
像下面一样更改你的xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
把这些行放在java中
View empty = findViewById(R.id.emptyView);
btn_retry=(Button)empty.findViewById(R.id.btn_retry);
listView.setEmptyView(empty);
现在,您可以使用相同的xml布局在相同的Activity中编写onClickListener().
btn_retry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do the refresh thing or verify with Toast.
}
});
在这里,如果listview为空,它将显示按钮刷新.否则它将显示填充列表.
标签:android,listview
来源: https://codeday.me/bug/20190723/1509860.html